Invoke-Command not passing variable to target machine
06:49 30 Oct 2026

I'm building a powershell script to uninstall a number of outdated apps from remote computers. I retrieved the uninstallstring from the 64bit Uninstall registry key, replaced some wrong characters and stored the result in a $uninstall variable.

Then, I mean to execute that uninstall string (an Msiexec /x string) through an Invoke-Command command. First thing I tried was

Invoke-Command -ComputerName $computer -ScriptBlock { $uninstall }

But that didn't work. But if I do the same command from powershell replacing the variable with its content, it works. So I guessed it was a matter of remote computer not recognizing the variable, and started checking scope of variables on powershell. I've tried:

Invoke-Command -ComputerName $computer -ScriptBlock { $using:uninstall }

But it does not uninstall the app on the target computer. The uninstall command is

MsiExec.exe /X`{F727DA6E-26F8-44A9-816F-4F51861C5149`} /qn /quiet /norestart

How can I properly pass this command stored in the local variable to the target computer or troubleshoot why it isn't working from the script?

powershell variables scope invoke-command