How do I pass variables with the Invoke-Command cmdlet?

Alternatively you can use the $Using:scope. See Example 5 under this link.

Example:

$servicesToSearchFor = "*"
Invoke-Command -ComputerName $computer -Credential (Get-Credential) -ScriptBlock { Get-Service $Using:servicesToSearchFor }

With $Using: you don’t need the -ArgumentList parameter and the param block in the scriptblock.

Leave a Comment