How do I replace an array with multi values with a specific value?

Mike 161 Reputation points

Example: Name:Mario Samaccountname:sMario Name:Luigi SamaccountName:sLuigi I want to replace sLuigi with xLuigi.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,891 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Mike 161 Reputation points

    and how to I remove Name:Mario Samaccountname:sMario

    0 comments No comments

  2. Mike 161 Reputation points

    if I save my variable to txt, it looks like this.

    DisplayName: Mario
    Name: Mario S.
    email: MarioS@xyz .com
    samaccountName: marios

    Displayname: Luigi
    Name: Luigi S.
    email: LuigiS@xyz .com
    samaccountName: luigis

    I 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 |
    $h.($.tSamaccountname) = $.gSamaccountname

  3. Ian Xue (Shanghai Wicresoft Co., Ltd.) 19,061 Reputation points Microsoft Vendor

    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 {  
            $_.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.