Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape spaces in PowerShell and cmd

Tags:

powershell

I have a PowerShell script that is calling an .exe from the command line to create an XML file and then the PowerShell script is reading that file.

The problem is if I have a space in the file path I need to wrap in in double quotes to pass it to the command line. When I do that the PowerShell command tries to read the double quotes as part of the file path. Is there a way to escape the spaces that will work for both passing it to the command line and using commands inside PowerShell?

It seems silly to have to pull out the double quotes for one command and leave them in for another.

The issue is with the Get-Content line not liking the double quotes.

$outputpath = '"C:\Users\bob\Desktop\output new.xml"'
Start -FilePath $sqlpackage -ArgumentList "/action:DeployReport /SourceFile:$dacpacpath /Profile:$profilepath /OutputPath:$outputpath" -NoNewWindow -Wait

[xml]$xmldocument = Get-Content -Path $outputpath
like image 755
ryanw55 Avatar asked Feb 25 '16 15:02

ryanw55


People also ask

How do I escape space in CMD?

In the Command Prompt, the caret character ( ^ ) will let you escape spaces—in theory. Just add it before each space in the file name. (You'll find this character in the number row on your keyboard. To type the caret character, press Shift+6.)

How do you pass a path with spaces in CMD?

Use quotation marks when specifying long filenames or paths with spaces. For example, typing the copy c:\my file name d:\my new file name command at the command prompt results in the following error message: The system cannot find the file specified. The quotation marks must be used.

How do you pass a space in command line arguments?

Note : You pass all the command line arguments separated by a space, but if argument itself has a space then you can pass such arguments by putting them inside double quotes “” or single quotes ”.

How do I open a directory with spaces in PowerShell?

If you need to change to a directory that has a space, you'll need to enclose it in quotes. Single, or double quotes will work. Or you can escape the space with the backtick character.


2 Answers

Deadly-Bagel almost had it I think. Don't use double quotes in $outputpath to keep Get-Content happy, but add them in your argument list string.

Be sure to escape with backtick `. In fact you might just want to do that with all the paths:

$outputpath = 'C:\Users\bob\Desktop\output new.xml'
Start -FilePath $sqlpackage -ArgumentList "/action:DeployReport /SourceFile:`"$dacpacpath`" /Profile:`"$profilepath`" /OutputPath:`"$outputpath`"" -NoNewWindow -Wait

[xml]$xmldocument = Get-Content -Path $outputpath
like image 190
Josh Petitt Avatar answered Sep 17 '22 13:09

Josh Petitt


-ArgumentList accepts String[] so try the following:

$outputpath = "C:\Users\bob\Desktop\output new.xml"

Start -FilePath $sqlpackage -ArgumentList @("/action:DeployReport", "/SourceFile:$dacpacpath", "/Profile:$profilepath", "/OutputPath:$outputpath") -NoNewWindow -Wait

I suspect passing it all as one parameter causes it to be confused.

like image 30
Deadly-Bagel Avatar answered Sep 20 '22 13:09

Deadly-Bagel