Escape spaces in PowerShell and cmd

Question:

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.

Answer:

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:

Source:

Escape spaces in PowerShell and cmd by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply