Selecting First 3 objects in an array

Welcome Forums General PowerShell Q&A Selecting First 3 objects in an array

This topic contains 11 replies, has 6 voices, and was last updated by

 
Participant
1 year, 3 months ago.

  • Author
    Posts
  • #74848

    Participant
    Points: 20
    Rank: Member

    Hi All

    not something ive really needed to do before however i need to rename the (Name) displayed in ADUAC, managed to find the correct CMDLETs however i dont just want to press the button, would like to try on a few to test first. -Whatif is fine but i cant see what the name changed to / if it changed correctly.

    so i would like to select the first 3 users in $users rename those and check

    
    $users = get-aduser -Filter * -SearchBase “OU=Users,OU=kuala Lumpur,OU=Malaysia,OU=Australasia,DC=global,DC=Domain,DC=com” -SearchScope Subtree -Properties displayname 
    
    foreach ($user in $users)
    {
        Rename-ADObject $user.DistinguishedName -NewName $user.displayname -WhatIf
    }
    
    
  • #74856

    Participant
    Points: 0
    Rank: Member

    Pipe get ad users to select -first 3:

     get-aduser -filter * | select -first 3
  • #74859

    Participant
    Points: 20
    Rank: Member

    Thanks Ben, nice and simple 🙂

    • #74880

      Participant
      Points: 0
      Rank: Member

      No worries! Glad it helped you.

  • #74892

    Keymaster
    Points: 1,619
    Helping HandTeam Member
    Rank: Community Hero

    Although querying ALL users just for the first three that happen to come back is pretty brutal on the domain controller, from a performance perspective.

    • #74895

      Participant
      Points: 0
      Rank: Member

      Should be alright.

    • #74960

      Participant
      Points: 20
      Rank: Member

      hey Don

      i thought with "searchbase" it would only query the objects within scope ?

  • #74999
    js

    Participant
    Points: 202
    Helping Hand
    Rank: Participant

    or

    $users[0..2]

    or

    $users[0,1,2]
    • #75005

      Participant
      Points: 0
      Rank: Member

      You still need to assign $Users though. :/

    • #75076

      Participant
      Points: 1
      Rank: Member

      (get-aduser -filter * )[0..2]

  • #75079

    Participant
    Points: 1
    Rank: Member

    I can't test it right this second, but I think -ResultSetSize 3 would limit the query to just the first three found. That should reduce any pounding on the DC before it even starts.

    • #75092

      Participant
      Points: 20
      Rank: Member

      Yep -ResultSetSize 3 also works 🙂

The topic ‘Selecting First 3 objects in an array’ is closed to new replies.