How to run powershell command with parameter name is variable

I need to run the same command Get-QadUser, and Set-QadUserat the same property and its value, but my property is variable.

my code (Set doesn't work!):

$property = 'MobilePhone' # OK
$value = ($User | get-QadUser -IncludedProperties $property).$property # OK
$user | Set-QadUser -$PropertyName $NewValue # NOK
+4
source share
1 answer

In this case, you can use the PowerShell function called Splatting, as Don Jones himself explained here: Windows PowerShell: Splatting .

In this, you can define parameters as an object of the dictionary of Property and Value pairs, as shown below:

$parameters = @{$FirstPropertyName = $FirstValue; $SecondPropertyName = $SecondValue; $ThirdPropertyName = $ThirdValue}

You can then pass this on to your cmdlet using an operator @as shown below:

Set-QadUser @parameters

:

$property = 'MobilePhone' # OK
$value = ($User | get-QadUser -IncludedProperties $property).$property # OK

$parameters = @{$PropertyName = $NewValue}
$user | Set-QadUser @parameters # OK

: , PetSerAl . , .

+2

All Articles