How can I pass an argument to a PowerShell script?
Tested as working:
#Must be the first statement in your script (not coutning comments)param([Int32]$step=30) $iTunes = New-Object -ComObject iTunes.Applicationif ($iTunes.playerstate -eq 1){ $iTunes.PlayerPosition = $iTunes.PlayerPosition + $step}
Call it with
powershell.exe -file itunesForward.ps1 -step 15
Multiple parameters syntax (comments are optional, but allowed):
<# Script description. Some notes.#>param ( # height of largest column without top bar [int]$h = 4000, # name of the output image [string]$image = 'out.png')
You can use also the $args
variable (that's like position parameters):
$step = $args[0]$iTunes = New-Object -ComObject iTunes.Applicationif ($iTunes.playerstate -eq 1){ $iTunes.PlayerPosition = $iTunes.PlayerPosition + $step}
Then it can be called like:
powershell.exe -file itunersforward.ps1 15
Call the script from a batch file (*.bat) or CMD
pwsh.exe -NoLogo -ExecutionPolicy Bypass -Command "./Script.ps1 -Param1 Hello -Param2 World"pwsh.exe -NoLogo -ExecutionPolicy Bypass -Command "path-to-script/Script.ps1 -Param1 Hello -Param2 World"pwsh.exe -NoLogo -ExecutionPolicy Bypass -Command "./Script.ps1 Hello -Param2 World"pwsh.exe -NoLogo -ExecutionPolicy Bypass -Command "./Script.ps1 Hello World"pwsh.exe -NoLogo -ExecutionPolicy Bypass -Command "./Script.ps1 -Param2 World Hello"
PowerShell
powershell.exe -NoLogo -ExecutionPolicy Bypass -Command "./Script.ps1 -Param1 Hello -Param2 World"powershell.exe -NoLogo -ExecutionPolicy Bypass -Command "path-to-script/Script.ps1 -Param1 Hello -Param2 World"powershell.exe -NoLogo -ExecutionPolicy Bypass -Command "./Script.ps1 Hello -Param2 World"powershell.exe -NoLogo -ExecutionPolicy Bypass -Command "./Script.ps1 Hello World"powershell.exe -NoLogo -ExecutionPolicy Bypass -Command "./Script.ps1 -Param2 World Hello"
Call from PowerShell
PowerShell Core or Windows PowerShell
& path-to-script/Script.ps1 -Param1 Hello -Param2 World& ./Script.ps1 -Param1 Hello -Param2 World
Script.ps1 - Script Code
param( [Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$false)] [System.String] $Param1, [Parameter(Mandatory=$True, Position=1, ValueFromPipeline=$false)] [System.String] $Param2)Write-Host $Param1Write-Host $Param2