Forum Discussion

Paganasia's avatar
Paganasia
Copper Contributor
Oct 12, 2019

Replacing property with variable

Hi,
I've made a CustomPSObject with many properties (36 for now) for new AD User. I want to pass through all of those and apply them in order to update a user information.
 
I was wondering if it's possible to replace the property name following the "-" with a variable. Here's a part of my script:
 
$UserObjectProperty = $UserObject| Get-Member -MemberType NoteProperty
$UserObjectProperty | ForEach-Object{
$PropertyToCheck = $_.name
$NewPropertyValue = $UserObject.($_.name)
$PreviousInfo = Get-ADUser $UserObject.SamAccountName -Properties * |Select-Object $PropertyToCheck
Set-Aduser $UserObject.SamAccountName -$PropertyToCheck "$NewPropertyValue"
}
 
So $PropertyToCheck can be anything like Description or Title or Manager for example. I'm able to get the PreviousInfo for logging purpose fine. It's crashing in the last step for now.
Is there a way to avoid having to put each different property with it's own line of Set-ADUser?
 
Hoping I'm clear enough to start with.
Thanks,
  • Just create a simple function that accepts a parameter for the property name and another one for the value, then pass them along to the function.

Resources