and how to I remove Name:Mario Samaccountname:sMario
How do I replace an array with multi values with a specific value?
Example: Name:Mario Samaccountname:sMario Name:Luigi SamaccountName:sLuigi I want to replace sLuigi with xLuigi.
3 answers
Sort by: Most helpful
-
-
Mike 251 Reputation points
2021-03-09T21:14:53.573+00:00 if I save my variable to txt, it looks like this.
DisplayName: Mario
Name: Mario S.
email: MarioS@xyz .com
samaccountName: mariosDisplayname: Luigi
Name: Luigi S.
email: LuigiS@xyz .com
samaccountName: luigisI am hoping to update it if a samaccountname exist in my hash
$x= [PSCustomObject]@{tSamaccountname='luigis';gSamaccountname='marios'}, [PSCustomObject]@{tSamaccountname='newLuigi';gSamaccountname='newMario'}
$h = @{}
$x |
ForEach-Object{
$h.($.tSamaccountname) = $.gSamaccountname
} -
Ian Xue 39,096 Reputation points Microsoft Vendor
2021-03-10T06:22:22.15+00:00 Hi @Mike ,
Is the variable an array of PSCustomObjects with the properties DisplayName, email, Name and samaccountName? If so, you can do something like the following
$var | ForEach-Object { if($h[$_.samaccountName]){ $_.samaccountName = $h[$_.samaccountName] } }
It will replace the value 'luigis' of the samaccountName property with the value 'mario' to the key 'luigis' of your hashtable $h.
Best Regards,
Ian Xue============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.