Selecting First 3 objects in an array

Tagged: 

This topic contains 11 replies, has 6 voices, and was last updated by Profile photo of Mark Prior Mark Prior 1 week, 4 days ago.

  • Author
    Posts
  • #74848
    Profile photo of Mark Prior
    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
    Profile photo of Ben Camareno
    Ben Camareno
    Participant

    Pipe get ad users to select -first 3:

     get-aduser -filter * | select -first 3
  • #74859
    Profile photo of Mark Prior
    Mark Prior
    Participant

    Thanks Ben, nice and simple 🙂

    • #74880
      Profile photo of Ben Camareno
      Ben Camareno
      Participant

      No worries! Glad it helped you.

  • #74892
    Profile photo of Don Jones
    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
      Profile photo of Ben Camareno
      Ben Camareno
      Participant

      Should be alright.

    • #74960
      Profile photo of Mark Prior
      Mark Prior
      Participant

      hey Don

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

  • #74999
    Profile photo of js
    js
    Participant

    or

    $users[0..2]

    or

    $users[0,1,2]
    • #75005
      Profile photo of Ben Camareno
      Ben Camareno
      Participant

      You still need to assign $Users though. :/

    • #75076
      Profile photo of Chris Bakker
      Chris Bakker
      Participant

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

  • #75079
    Profile photo of Rob Dowell
    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
      Profile photo of Mark Prior
      Mark Prior
      Participant

      Yep -ResultSetSize 3 also works 🙂

You must be logged in to reply to this topic.