How to use a PowerShell variable as command parameter? How to use a PowerShell variable as command parameter? powershell powershell

How to use a PowerShell variable as command parameter?


Typically to use a variable to populate cmdlet parameters, you'd use a hash table variable, and splat it, using @

 $newVar = @{option1 = $true} mycommand @newVar

Added example:

$AS_policy1 = @{Identity = "asdf"AllowBluetooth = $true}Set-ActiveSyncMailboxPolicy @AS_policy1


See if this works for you:

 iex "MyCommand -$($newVar):$true"


I had the same Problem and just found out how to resolve it. Solution is to use invoke-Expression: invoke-Expression $mycmdThis uses the $mycmd-string, replaces variables and executes it as cmdlet with given parameters