Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass an argument to a PowerShell script?

There's a PowerShell script named itunesForward.ps1 that makes iTunes fast forward 30 seconds:

$iTunes = New-Object -ComObject iTunes.Application  if ($iTunes.playerstate -eq 1) {   $iTunes.PlayerPosition = $iTunes.PlayerPosition + 30 } 

It is executed with a prompt line command:

powershell.exe itunesForward.ps1 

Is it possible to pass an argument from the command line and have it applied in the script instead of the hardcoded 30 seconds value?

like image 368
Boris Pavlović Avatar asked Apr 08 '11 08:04

Boris Pavlović


People also ask

How do I call a PowerShell script with parameters?

You can run scripts with parameters in any context by simply specifying them while running the PowerShell executable like powershell.exe -Parameter 'Foo' -Parameter2 'Bar' . Once you open cmd.exe, you can execute a PowerShell script like below.

How do I pass multiple arguments to a PowerShell script?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.

What is param () in PowerShell?

The PowerShell parameter is a fundamental component of any script. A parameter is a way that developers enable script users to provide input at runtime. If a PowerShell script's behavior needs to change in some way, a parameter provides an opportunity to do so without changing the underlying code.

How do you use parameters in PowerShell?

The name of the parameter is preceded by a hyphen ( - ), which signals to PowerShell that the word following the hyphen is a parameter name. The parameter name and value can be separated by a space or a colon character. Some parameters do not require or accept a parameter value.


1 Answers

Tested as working:

#Must be the first statement in your script (not coutning comments) param([Int32]$step=30)   $iTunes = New-Object -ComObject iTunes.Application  if ($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' ) 

And some example for advanced parameters, e.g. Mandatory:

<#     Script description.      Some notes. #> param (     # height of largest column without top bar     [Parameter(Mandatory=$true)]     [int]$h,          # name of the output image     [string]$image = 'out.png' )  Write-Host "$image $h" 

A default value will not work with a mandatory parameter. You can omit the =$true for advanced parameters of type boolean [Parameter(Mandatory)].

like image 54
Ocaso Protal Avatar answered Oct 19 '22 17:10

Ocaso Protal