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"


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


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

3 Responses 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”

    Liked by 1 person

    • Nom De Guerre says:

      Henk, thanks for leaving that comment! It was helpful for a scenario where a user downloads/extracts an archive (possibly to a path including spaces) containing several resources including a batch file and PowerShell script. The user is expected to run (double-click, or right-click and select “Run as administrator”) the simple one-liner batch file, something like:
      powershell.exe -ExecutionPolicy ByPass -NonInteractive -NoProfile -WindowStyle Hidden -File “%~dp0ScriptName.ps1”

      %~dp0 refers to the drive and path of the batch file being executed. This allows the batch file to work as expected, even if the user opens a command prompt (perhaps with current working directory of C:\Windows\System32), and manually enters the full path/filename of the batch file at the prompt. My earlier attempts to use -Command or the file path/name by itself failed in cases where the path to the extracted files included spaces.


  2. shad hasan says:

    That is really helpful guideline. Seems like bug.


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.