14

I was wondering if someone with more expertise could help me with a little problem I'm having with using Variables in -ArgumentList when using Start-Process.

If I run the Exe without using Start-Process

.\DeploymentServer.UI.CommandLine.exe register --estNumber $Number --postcode $PostCode --password $Password

everything works fine, the command runs and the software is registered.

If I try

Start-Process .\DeploymentServer.UI.CommandLine.exe -ArgumentList "register --estNumber $Number --postcode $PostCode --password $Password" -Wait -NoNewWindow

or

$Arguments = "register --estNumber $Number --postcode $PostCode --password $Password"
Start-Process .\DeploymentServer.UI.CommandLine.exe -ArgumentList $Arguments -NoNewWindow -Wait

the command runs but is unable to register, stating that it can not match the details provided. So I'm assuming the issue lies either in the passing of the arguments to Start-Process, or -ArgumentList interpreting the variables in the string. Am I missing something really simple here? Possibly to do with the $ in the -ArgumentList?

3
  • What are the "details provided"? Does it help if you provide the argument list as an array? Commented Jan 31, 2016 at 17:59
  • the details held in the 3 variables "$number = 999999" "$postcode = a12 1ab" "$Password =password". There is nothing spectacular about any of them. They work fine when not using Start-Process Commented Jan 31, 2016 at 18:03
  • 1
    Start-Process never add quotes for you, while PowerShell normally try to be smart and add quotes, when arguments contains spaces: $Text='With Space';start cmd "/c echo $Text" -NoNewWindow -Wait;cmd /c echo $Text. Commented Jan 31, 2016 at 18:04

2 Answers 2

18

You have a space in your $postcode, so you need to put the argument in quotes:

Start-Process .\DeploymentServer.UI.CommandLine.exe -ArgumentList "register --estNumber $Number --postcode `"$PostCode`" --password $Password" -Wait -NoNewWindow
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, I'll give that a try. Is there any reason you know why there would be a problem with only when using Start-Process? I'm hoping to try to understand why it might be interpreting it differently then running it "natively"
As @PetSerAl mentioned, PowerShell handles variables smartly when it runs statements itself. But here you pass an argument string to Start-Process where PowerShell only fills in the variables and Start-Process takes the string and runs the command.
"You have a space in your $postcode" What space is this referring to? I see no space in the variable name $postcode.
4

I encountered several posts online saying to wrap a Start-Process -ArgumentList argument containing spaces in 2 layers of doubles quotes, escaping the inner double quotes with a back tick `, but at least in the context I needed it, that didn't work. I found a conceptually similar solution which did work, however, i.e. a set of single quotes on the outside and a "traditional" backslash escape sequence on inner double quotes. I was guided to using this approach per this PowerShell issue post:

https://github.com/PowerShell/PowerShell/issues/5576

This example works for me (running a PS Start-Process command from cmd.exe):

C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe Start-Process -FilePath 'C:\Program Files (x86)\Example\Test.exe' -ArgumentList 'arg1','\"arg 2 w spaces\"','arg3'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.