PowerShell recognizes $SnapinInstaller as a variable and evaluates it for the string. If you want PowerShell to βinvokeβ a command called a variable, you use the & call operator as follows:
& $SnapinInstaller /qn /l* $SnapinInstallLog
While you can use Invoke-Expression , this is usually avoided, especially with user-provided data, due to the possibility of a script attack, for example:
PS> $SnapinInstallLog = Read-Host "Enter log file name" Enter log file name: c:\temp\snapin.log; remove-item C:\xyzzy -r -force -whatif PS> Invoke-Expression "$SnapinInstaller /qn /l* $SnapinInstallLog"
The problem occurs here because the user was able to enter an arbitrary script.
OTOH, if you have bunch arguments presented on the same line (and without user input), Invoke-Expression can come in handy in this scenario, for example:
$psargs = "\\$computer -d -i 0 notepad.exe" Invoke-Expression "psexec.exe $psargs"
source share