Selecting First 3 objects in an array

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

Pipe get ad users to select -first 3:

 get-aduser -filter * | select -first 3

Thanks Ben, nice and simple :slight_smile:

No worries! Glad it helped you.

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.

Should be alright.

hey Don

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



You still need to assign $Users though. :confused:

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

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.

Yep -ResultSetSize 3 also works :slight_smile: