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?

Tested as working:

param([Int32]$step=30) #Must be the first statement in your script

$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

How can I pass an argument to a PowerShell script?, You can pass parameters to a PowerShell script two different ways. (If you don't know about PowerShell, please see the tip, "Introduction to� Pass arguments by named parameters. In unnamed parameters method, you cannot have more control with inputs and powershel script itself look unclear to understand the process. To overcome this, you can pass arguments by named parameter. To get arguments by name, we need to use param statement.

You can use also the $args variable (that's like position parameters):

$step = $args[0]

$iTunes = New-Object -ComObject iTunes.Application

if ($iTunes.playerstate -eq 1)
  $iTunes.PlayerPosition = $iTunes.PlayerPosition + $step

Then it can be called like:

powershell.exe -file itunersforward.ps1 15

Passing Parameters to a PowerShell Script (Tips.Net), Edit your script and at the head of your file before everything add the following lines: The statement: Param(): declares parameters that will be� I created a simple script called hello.ps1 by typing in (PS) "notepad hello.ps1" It contains a command called gci (get-childitem). After entering the command in noted pad I saved it. I have heard that we can pass arguments to the script name in PS. Therefore, my Question is how can I filter the gci command I created. The command below that I

Call the script from a batch file (*.bat) or CMD

PowerShell Core

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"


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"

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

    [Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$false)]

    [Parameter(Mandatory=$True, Position=1, ValueFromPipeline=$false)]

Write-Host $Param1
Write-Host $Param2

How to pass arguments to a powershell script?, How-to: Pass parameters to a PowerShell script. Script Parameters / Arguments ( for scripts, functions and script blocks). Pass arguments to a script or cmdlet by� Ansible - How to pass an array as argument to a powershell script. Ask Question Asked 2 years, How can I pass an argument to a PowerShell script? 954.

Let PowerShell analyze and decide the data type. It internally uses a 'Variant' for this.

And generally it does a good job...

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

Or if you need to pass multiple parameters:

param($x1, $x2)
$iTunes = New-Object -ComObject iTunes.Application
if ($iTunes.playerstate -eq 1)
    $iTunes.PlayerPosition = $iTunes.PlayerPosition + $x1
    $iTunes.<AnyProperty>  = $x2

PowerShell Parameters - PowerShell, I want later the script to be executed with parameters like so: powershell.exe .\ scripts\archiver.ps1 -30 -c:\test -"*.xml". Do i have to replace the value with the� I am new to powershell, and trying to teach myself the basics. I need to write a ps script to parse a file, which has not been too difficult. Now I want to change it to pass a variable to the script. that variable will be the parsing string. Now, the variable will always be 1 word, and not a set of words or multiple words.

Create a PowerShell script with the following code in the file.

Get-ChildItem $path | Where-Object {$_.LinkType -eq 'SymbolicLink'} | select name, target

This creates a script with a path parameter. It will list all symbolic links within the path provided as well as the specified target of the symbolic link.

Passing parameters to a script - PowerShell General, To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample� If you have anything inside test.ps1, you have to parametrize and pass the value. You don’t need to use the call operator (&), just remove the quotes. If you are executing only that script in scriptblock. You can use -FilePath parameter of Invoke-Command.

How To: Pass arguments to a PowerShell Script using the Run , When you run this script as a .ps1 file, you can use parameter names and values. PS C:\> .\New-WordTree.ps1 -Word PowerShell -Number 3� I want to have a powershell script that can look at a text file and pass the results as parameters to the exe. So the text document would look like this : Hostname = Server 12. So I'm hoping the - hostname argument it can be passed to the exe with a result of server 12.

Passing Parameters to a Script in an Executable File – SAPIEN Blog, Q: What is the best way of passing parameters to my Windows PowerShell script? John Savill | Jan 01, 2013. A: Windows PowerShell has an easy way to pass� As you can see in the “Arguments” field we have to declare which Variables we want to pass to the PowerShell script as parameters: - ApiManagementServiceName $ ( ApiManagementServiceName ) - ApiManagementServiceResourceGroup $ ( ApiManagementServiceResourceGroup )

Passing parameters to PowerShell, A script that never changes and has static values may not need parameters, but to really improve your scripts, you will want to add parameters to� Script.ps1 -File NodeList.txt So the.txt file would be passed as an argument (and define the content of the array). Your way is fine and will work, but let me know if you know how to do it the way specified above.

  • what if the parameter is a string? What is the syntax? would it be something like -step '15' or -step "15"
  • @Andrew First of all you have to change the type of the parameter to [string]. If you then want to pass a string as parameter you can use either ' or ". If there is no space (or quotes) inside the string you can even omit the quotes.
  • FYI, to use multiple params, use this syntax: param([string]$env,[string]$s3BucketName)
  • It is missing "-file". It doesnt work for me until i added this. See the complete code: powershell.exe -file itunesForward.ps1 -step 15
  • @Charles Thanks for the hint. You are right: The -file is missing from the call. The call without might work with Powershell Version 1.0 but I can't test it. Updated the answer.
  • Found this easier than the accepted solution, PLUS you can directly use $args[0] anywhere in the script (no need to be first line). PS: Tip on passing strings as arguments: They must be enclosed in single quotes.
  • Both this and the accepted solution work, the main difference is that this reads parameters by position, while the accepted solution does it by name. When multiple parameters need to be passed, passing by name might be cleaner.
  • named params in accepted solution also auto populate get-help
  • This answer is getting so much attention, please check out the related one which is much more complete.…
  • It's works .\create-application-pool.ps1 -site xx_8010 -application AppTest -dirtestDir -applicationPool TestAppPool