Get All users in a specific OU

This comes quite handy in an organization where OU structure is properly maintained
or even in a messy organization if you are trying to sort it.

$ou = "OU you want to get the users from"
$arr = @()
$pathtosave = "path to save.csv"

Add-Content -Path $pathtosave -Value '"First Name","Last Name","SAM Name","Description","OU"'

$arr = get-aduser -SearchBase $ou -Properties * -Filter * | select givenname,surname,samaccountname,Description,DistinguishedName,Enabled  |? {$_.enabled -eq $true}

foreach($unit in $arr){
Add-Content -Path $pathtosave -value ($unit.givenname+","+$unit.surname+","+$unit.samaccountname+","+$unit.Description+","+$unit.DistinguishedName)
}

Published by iamfazul

Author of the site

Leave a comment