Home > Software design >  double quote issue in powershell
double quote issue in powershell

Time:04-09

I am trying to pass installation arguments to a variable in powershell but I get errors doing that.

$InstallString = "$InstallLocation\application.exe" /install / quiet CID="BsDdfi3kj" Tag="CinarCorp"

I tried to run this as putting "&" symbol but it didn't work and I checked many websites but I couldn't solve it. Any help will be appreciated.

Thanks..

CodePudding user response:

The syntax used to the right of = only works when directly calling the command like this:

& "$InstallLocation\application.exe" /install /quiet CID="BsDdfi3kj" Tag="CinarCorp"

Note that you had a spurious space character before quiet which I removed.

Change the syntax like this when you actually want to store the command in a variable:

$InstallString = "`"$InstallLocation\application.exe`" /install /quiet CID=`"BsDdfi3kj`" Tag=`"CinarCorp`""

I have enclosed the whole string within double-quotes and escaped the inner double-quotes by placing a backtick in front of them.

You could also use a here-string to avoid having to escape the inner double-quotes:

$InstallString = @"
"$InstallLocation\application.exe" /install /quiet CID="BsDdfi3kj" Tag="CinarCorp"
"@

Note that the actual string as well as the final "@ have to start at the beginning of the line. If you indent the actual string, the spaces/tabs are included in the variable, which is usually not wanted.

You could of course trim the string if you insist on indentation:

$InstallString = @"
    "$InstallLocation\application.exe" /install /quiet CID="BsDdfi3kj" Tag="CinarCorp"
"@.Trim()

I recommend to read about Quoting Rules for further details.

  • Related