PowerShell and double quotes on the command line

I’ve had issues passing in parameters to a PowerShell script that need double quotes around them, due to containing a space. Consider the following very basic PowerShell script:

param([string]$Something = "")
Write-Host "I received $Something"

This just writes out what the script received as the parameter. I’ve saved the script with a filename of C:\Scripts\ParamCheck.ps1.

From a cmd prompt, execute the script as follows:

powershell.exe C:\Scripts\ParamCheck.ps1 -Something "one two"

gives:

I received one

Here’s some ways to make this work properly:

Use single quotes:

powershell.exe C:\Scripts\ParamCheck.ps1 -Something 'one two'

Use a backslash delimiter (even though PowerShell internally uses the left-single-quote `):

powershell.exe C:\Scripts\ParamCheck.ps1 -Something \"one two\"

Use three double quotes:

powershell.exe C:\Scripts\ParamCheck.ps1 -Something """one two"""

All three of the above give the expected output of:

I received one two

Done.

This entry was posted in PowerShell and tagged , , , , . Bookmark the permalink.

One Response to PowerShell and double quotes on the command line

  1. Henk Rook says:

    Try the following instead (prefixing the script with “-File”):
    powershell.exe -File C:\Scripts\ParamCheck.ps1 -Something “one two”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s