Selecting First 3 objects in an array

This topic contains 11 replies, has 6 voices, and was last updated by  Mark Prior 3 months ago.

  • Author
    Posts
  • #74848

    Mark Prior
    Participant

    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

    Ben Camareno
    Participant

    Pipe get ad users to select -first 3:

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

    Mark Prior
    Participant

    Thanks Ben, nice and simple 🙂

    • #74880

      Ben Camareno
      Participant

      No worries! Glad it helped you.

  • #74892

    Don Jones
    Keymaster

    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

      Ben Camareno
      Participant

      Should be alright.

    • #74960

      Mark Prior
      Participant

      hey Don

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

  • #74999

    js
    Participant

    or

    $users[0..2]

    or

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

      Ben Camareno
      Participant

      You still need to assign $Users though. :/

    • #75076

      Chris Bakker
      Participant

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

  • #75079

    Rob Dowell
    Participant

    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

      Mark Prior
      Participant

      Yep -ResultSetSize 3 also works 🙂

You must be logged in to reply to this topic.