Mike Caron
Mike Caron

Reputation: 5784

How do I feed a quoted argument to perl in Powershell?

I want to call a perl script from powershell where a parameter is quoted:

myProg -root="my path with spaces"

I've tried to use -root='"my path with spaces"', -root='my path with spaces', -root=\"my path with spaces\", but nothing seems to work. After pressing <ENTER>, I see >> as a prompt.

How do I pass this quoted argument on the command line in Powershell?

Upvotes: 1

Views: 1003

Answers (4)

0x574F4F54
0x574F4F54

Reputation: 415

I ran into a similar issue when trying to use powershell to pass arguments with spaces to an executable. In the end I found that I could get a quoted parameter passed by triple-escaping the closing double quote of the argument when using Invoke-Expression:

iex "&`"C:\Program Files\Vendor\program.exe`" -i -pkg=`"Super Upgrade```" -usr=User -pwd=password2"

What isn't apparent is why I can use a single back-tick character to escape the executable while I have to use 3 back-ticks to finish off a quoted parameter. All I know is that this is the only solution that worked for me.

Upvotes: 0

Peter Seale
Peter Seale

Reputation: 5015

It may be useful to explicitly denote each command-line argument. Instead of relying on the parser to figure out what the arguments are via whitespace, you explicitly create an array of strings, one item for each command-line argument.

$cmdArgs = @( `
    '-root="my path with spaces"', `
    'etc', `
    'etc')

& "C:\etc\myprog.exe" $cmdArgs

Upvotes: 1

Filburt
Filburt

Reputation: 18061

I solved a similar issue with

Invoke-Expression '&.\myProg.exe `-u:IMP `-p: `-s:"my path with spaces"'

Hope this helps.

Upvotes: 0

zdan
zdan

Reputation: 29450

Try putting the entire argument in quotes and escape the inner quotes, that way powershell won't try to parse it:

myProg '-root=\"my path with spaces\"'

Upvotes: 2

Related Questions