Running Scripts with arguments in PowerShell

Here’s a short guide on running scripts in powershell.

The guide is divided into three parts:

  • Running Scripts from the Console
  • Running Scripts from Start/Run
  • Running Scripts as Backgournd Jobs in PowerShell

First, let’s look at the script examples that we want to run.

Here’s the first script:

##################
#
# Script 1
#
##################

"Arguments: $($args.count)"
$args


And here’s the second script:

##################
#
# Script 2
#
##################

param($Argument1,$Argument2)

"Argument1 is: $Argument1"
"Argument2 is: $Argument2"

Script 1 uses $args variable to handle arguments. This is a automatic Variable created by PowerShell. It will handle any arguments that are passed to the script.

Script 2 uses a param statement. the param statement let’s us define variables that hold our arguments. The variables are available throughout the script.

Running Scripts from the Console

Now, let’s look at a couple of different ways of running scripts from the PowerShell console.


PS > C:\temp\Scripts\script1.ps1 Hello World

Arguments: 2
Hello
World

In the example, we triggered the script with two arguments, Hello and World. If we instead want Hello World to pass as a single argument, we can use quotation marks:


PS > C:\temp\Scripts\script1.ps1 “Hello World”

Arguments: 1
Hello World

In this example, There's only one argument passed to the script since "Hello World" is within quotation marks.

Obviously, we can use variables as arguments.


PS > $HelloWorld = "Hello World"
PS > $Hello = "Hello"
PS > $World = "World"

PS > C:\temp\Scripts\script1.ps1 $HelloWorld

Arguments: 1
Hello World

When we enter $HelloWorld, the value of the $HelloWorld variable is passed on as one argument to the script.

If we instead use the Variables $Hello and $World, they will be handled as two different arguments.


PS > C:\temp\Scripts\script1.ps1 $Hello $World

Arguments: 2
Hello
World

If we put the variables within quotation marks, they will be passed on to the script as one single argument.


PS > C:\temp\Scripts\script1.ps1 "$Hello $World"

Arguments: 1
Hello World

Using Single Quotation takes passes the argument exactly as written, in other words, the Argument will be $Hello $World and not the values of the variables.


PS > C:\temp\Scripts\script1.ps1 '$Hello $World'

Arguments: 1
$Hello $World

It's possible to achieve this when using Double Quotation marks. Just add a backtrick character in front of the variables.


PS > C:\temp\Scripts\script1.ps1 "`$Hello `$World"

Arguments: 1
$Hello $World

Let's look at a few examples on running Script 2 from the console. Script 2 uses the param statement.


PS > C:\temp\Scripts\script2.ps1 Hello World

Argument1 is: Hello
Argument2 is: World

In this example, Hello is assigned to $Argument1 and World is Assigned to $Argument2


PS > C:\temp\Scripts\script2.ps1 "Hello World"

Argument1 is: Hello World
Argument2 is:

If we put Hello World in double quotation marks, both Hello and World are assigned to $Argument1. $Argument2 is empty.

It's also possible to use the param variables as parameters to the script as the examples below show.


PS > C:\temp\Scripts\script2.ps1 -Argument1 Hello -Argument2 World

Argument1 is: Hello
Argument2 is: World

If we only specify one parameter the second will automatically be assigned to the first variable in the param that doesn't have a value.


PS > C:\temp\Scripts\script2.ps1 -Argument1 Hello World

Argument1 is: Hello
Argument2 is: World

PS > C:\temp\Scripts\script2.ps1 -Argument2 World Hello

Argument1 is: Hello
Argument2 is: World

You can, of course, use quotation marks here as well.


PS > C:\temp\Scripts\script2.ps1 -Argument1 "Hello World"

Argument1 is: Hello World
Argument2 is:

What if the Path to the script contains spaces ?. Let's see what happens. in these examples I've changed the path to the scripts to C:\Temp\Example Scripts\


PS > C:\temp\Example Scripts\script1.ps1 "Hello World"

The term 'C:\temp\Example' is not recognized as the name..

so this didn't work. It's because PowerShell separates commands with a space, so it tries to run the command C:\Temp\Example. You can solve this by using quotation marks and a Call Operator. without the Call Operator, PowerShell will handle the script path as a string value.


PS > & "C:\temp\Example Scripts\script1.ps1" "Hello World"

Arguments: 1
Hello World

Script2 can be triggered in the same way, note that the arguments are after the scriptpath and not within quotation marks.


PS > & "C:\temp\Example Scripts\script2.ps1" -Argument1 Hello -Argument2 World

Argument1 is: Hello
Argument2 is: World

Another way of running scripts is if you're in the same directory as the scripts are located. Then you can simply use the .\ notation in front of the script.


PS > cd 'C:\temp\Example Scripts\'
PS > .\script1.ps1 "Hello World"

Arguments: 1
Hello World

The easist way to run scripts is if you include the scriptpath in the environment PATH.


$Env:PATH = $Env:PATH + ";C:\Temp\Example Scripts"

Now we can simply type the name of the script to run it.


PS > script2.ps1 -Argument1 Hello -Argument2 World

Argument1 is: Hello
Argument2 is: World

You don't even have to type the script extension when running the scripts.


PS > script2 -Argument1 Hello -Argument2 World

Argument1 is: Hello
Argument2 is: World

Running Scripts from Start/Run

You can also trigger scripts from Start / Run in Windows. Here are a couple of examples on how we can run our scripts from Start / Run. The NoExit switch is used in these examples so that we can check the result easily.


powershell.exe -noexit C:\temp\Scripts\script1.ps1 Hello World

When running Scripts that are in a path that doesn't include spaces you can simply type the scripts FullPath followed by the arguments as shown in the example above. But what if you want "Hello World" to be handled as just one argument?


powershell.exe -noexit C:\temp\Scripts\script1.ps1 "Hello World"

If we type "Hello World" inside double quotation marks it's still handled as two different arguments. We can solve this by using Single Quotation Marks.


powershell.exe -noexit C:\temp\Scripts\script1.ps1 'Hello World'

Let's see how this works with the script that uses a param.


powershell.exe -noexit C:\temp\Scripts\script2.ps1 Hello World

This works just fine, lets specify the parameters in the script as well.


powershell.exe -noexit C:\temp\Scripts\script2.ps1 -Argument1 Hello -Argument2 World

And, as you can see, this works fine as well. Now let's see what happens if we double quote on of the arguments.


powershell.exe -noexit C:\temp\Scripts\script2.ps1 -Argument1 "Hello World" -Argument2 Monkey

This returns Hello as Argument1 and Monkey as Argument2. World is not in the script ouput. Now let's do the same example with single quotation marks.


powershell.exe -noexit C:\temp\Scripts\script2.ps1 -Argument1 'Hello World' -Argument2 Monkey

Now we got the result that we wanted. So lesson learned from this is: use single quotation marks when passing arguments containing spaces.

Now let's test this with the scripts containing spaces in the script path.


powershell.exe -noexit C:\temp\Example Scripts\script1.ps1 "Hello World"

This didn't work since the path contains spaces, now let's try it with double quotes at first.


powershell.exe -noexit "C:\temp\Example Scripts\script1.ps1" "Hello World"

Still no luck, lets try adding the Call Operator.


powershell.exe -noexit & "C:\temp\Example Scripts\script1.ps1" "Hello World"

In order to get it right, we have to use single quotation marks.


powershell.exe -noexit & 'C:\temp\Example Scripts\script1.ps1' "Hello World"

Note that Hello World is handled as two different arguments. Whe can solve this by typing Hello World within single quotation marks.


powershell.exe -noexit & 'C:\temp\Example Scripts\script1.ps1' 'Hello World'

Script 2, that uses the param statement works in the same way.


powershell.exe -noexit & 'C:\temp\Example Scripts\script2.ps1' -Argument1 Hello -Argument2 World

powershell.exe -noexit & 'C:\temp\Example Scripts\script2.ps1' -Argument1 'Hello World'  -Argument2 Monkey

Finally, let's see what happens if we run scripts included in the $Env:PATH variable.

First, we should add the script path to our profile so that it's loaded when powershell starts. Just Add $Env:PATH = $Env:PATH + ";C:\Temp\Example Scripts" into your profile.ps1 file.


powershell.exe -noexit script1.ps1 'Hello World'

powershell.exe -noexit script2.ps1 -Argument1 Hello -Argument2 World

We can even run the scripts without the extension.


powershell.exe -noexit script1 'Hello World'

powershell.exe -noexit script2 -Argument1 Hello -Argument2 World

Adding a script path to the $Env:PATH variable makes it alot more simple to run scripts in PowerShell.

Running Scripts as Backgournd Jobs in PowerShell

Let's take a look on how to run scripts as background Jobs. PowerShell v2 includes a great cmdlet callet Start-Job. It let's us run Jobs in the background. We can use this to run our scripts as background jobs.

Let's take a look on How to Run Scripts witth Start-Job.


PS > $Job1 = Start-Job .\script1.ps1 -ArgumentList Hello,World

By placing the command in a variable, we can easily access the Job informationm, check for status and use it to retrieve the output of the command. Start-Job Doesn't return the Job Result when completed so we have to use Receive-Job to get the Result from the Script.


PS > Receive-Job $Job1.ID
Arguments: 2
Hello
World
PS > Receive-Job $Job1.ID
PS >

When we run the Receive-Job CmdLet and use the Jobs ID as argument, we get the Result from the Job Returned. Note that if we run the same command again, we don't get anything returned. This is becauese Receive-Job, by default, deletes the Returned information after it's accessed. We can bypass this with the -Keep switch.


PS > $Job1 = Start-Job .\script1.ps1 -ArgumentList Hello,World
PS > Receive-Job $Job1.ID -Keep
Arguments: 2
Hello
World
PS > Receive-Job $Job1.ID -Keep
Arguments: 2
Hello
World

To Close the Session, run the Receive-Job command without the -Keep switch or use the Remove-Job CmdLet as shown below.


PS > Remove-Job $Job1.ID

So how to solve this without PowerShell V2 installed ? well, we could always use Wscript.Shell. Here's an example on running backgroundjobs with wscript.shell.


PS > $Wscript = New-Object -Com Wscript.Shell
PS > $Command = "powershell.exe & 'C:\temp\Example Scripts\script2.ps1' -Argument1 'Hello' -Argument2 'World'"
PS > $Wscript.Run($command,0,$False)
0

So how do we varify that the script actually runs ? Well, we can solve this with a simple pipeline.


PS > $Wscript = New-Object -Com Wscript.Shell
PS > $OutFile = "C:\temp\output.txt"
PS > $Command = "powershell.exe & 'C:\temp\Example Scripts\script2.ps1' -Argument1 'Hello' -Argument2 'World' | Out-File $OutFile"
PS > $Wscript.Run($command,0,$False)

0

PS > Get-Content $OutFile

Argument1 is: Hello
Argument2 is: World

Hope you find this guide useful.

Rating 4.56 out of 5
[?]

4 thoughts on “Running Scripts with arguments in PowerShell

  1. Pingback: PowerShell.nu » Blog Archive » Running Scripts with arguments in … Scripts Rss

  2. I’m sure I used “powershell.exe & ‘C:\path_to\script.ps1′” successful, but now I have no idea why this no more works. Really disappointing, as there no alternative.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Anti-Spam Protection by WP-SpamFree