How to pass arguments to PowerShell scripts

How to pass arguments to PowerShell scripts

You can pass arguments to PowerShell scripts.

This is almost the same as at the command prompt.

They are passed in the script as $args[0],$args[1]… … and so on.

Save as a.ps1 below.

Write-Host $args[0]
Write-Host $args[1]

Run this script.

PS C:\work_ps> ./a.ps1 a b
a
b
PS C:\work_ps>

If there are no arguments on the command line, they are ignored.

The first argument is $args[0]. Then 1,2,3… and so on.

コメント

タイトルとURLをコピーしました