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

Mike 251 Reputation points
2021-03-09T18:28:09.857+00:00

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.
5,605 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Mike 251 Reputation points
    2021-03-09T18:31:11.697+00:00

    and how to I remove Name:Mario Samaccountname:sMario

    0 comments No comments

  2. 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: 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 |
    ForEach-Object{
    $h.($.tSamaccountname) = $.gSamaccountname
    }


  3. 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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.