Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell and a path as an argument that is double-quote delimited

I have a simple PS script that needs to accept a parameter that happens to be a path to a directory. I get this path handed to me and invoke the ps script as follows:

powershell.exe -ExecutionPolicy Bypass -F "C:\temp\ctest\logging test\postinstall.ps1" "C:\temp\ctest\logging test\"

I cannot control the addition of the '\' to the path that is the param to this script, and it must be double-quoted to account for the space in the path. So, what I wind up with is a variable inside my ps script that is the string:

C:\temp\ctest\logging test"     <<-- error in path!  with the double-quote char. :(

My question is simple, I hope, yet I cannot locate anyone who has solved it, yet. Is there no way to tell powershell not to escape that last double-quote in this scenario?

Thank you for your time, and for educating me.

like image 270
joebalt Avatar asked Feb 16 '12 19:02

joebalt


1 Answers

The issue looks to be only when invoked from CMD. In your script you could do this:

$args[0].TrimEnd('"')

It will remove a trailing double quote if one exists.

Or you could double up the backslash:

C:\>powershell.exe -f C:\echo.ps1 "C:\temp\ctest\logging test\\"

Contents of echo.ps1

Write-Host ('"{0}"' -f $args[0])
like image 110
Andy Arismendi Avatar answered Sep 30 '22 16:09

Andy Arismendi