Monday, June 27, 2016

Using powershell to call arguments with no spaces

Sometimes you have an application (like 7 Zip) that uses command line switches without a space between the switch and the value.  If you're using a variable for the value, sometimes PowerShell won't parse the values out right, so the thing will fail.

In this case, double quotes are your friend.  So instead of something like this:

path\to\7z.exe -w$tempDir -tzip -mx1 a ....

you'd do this:

path\to\7z.exe "-w$tempDir" -tzip -mx1 a ...

PowerShell will parse between the double quotes to replace the $tempDir variable appropriately.

You can, FWIW, also escape the hyphen with a back tick ( ` ) to do the same thing.  I happen to think the quotes option is more readable, but either works well.

path\to\7z.exe `-w$tempDir -tzip -mx1 a ...


No comments:

Post a Comment

Thanks for leaving a comment!