0

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..

2
  • where did you position the & ? Could you share an example using it ? Commented Apr 8, 2022 at 20:34
  • Added just before "$InstallLocation...." line Commented Apr 8, 2022 at 20:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.