Problem:
How do I return the sAMAccountName and a particular attribute – in this case extensionAttribute1 for all Active Directory users in PowerShell
Solution:
Run the below command:
Get-ADUser -Properties extensionAttribute1 -Filter * | Select sAMAccountName, extensionAttribute1 | export-csv c:\temp\extensionattribute1.csv
Works like a charm. Many thanks man. We wanted to obtain a list of all Executives. Now we use this command, export it into Excel and sort it . Done.
Can I target the users in a specific OU?
Derek
Yes, by using the -SearchBase parameter.
First you need the Distinguished Name (DN) of the OU you want to specify. To get a list of all of your OUs with PowerShell:
$OUs = Get-ADOrganizationalUnit -Filter *
$OUs | Format-Table Name,DistinguishedName
Or you can right-click on the OU you want in AD Users and Computers , choose properties then, in the Attribute Editor tab, double-click on distinguishedName from where you can copy it to the clipboard.
Now that you’ve got the DN, you can use it in the Get-ADUser cmdlet like so:
get-aduser -SearchBase “OU=Users,OU=Marketing,DC=yourdomain,DC=int” -filter *
Or, if you’ve assigned the OU’s DN to a variable called $OU with
$ou=”OU=Users,OU=Marketing,DC=yourdomain,DC=int”)
like so…
get-aduser -SearchBase $ou -filter *
(by the way, the -SearchBase parameter can be used with Get-ADOrganizationalUnit as well)
Jeff
Hi there,
I have a list of users in a csv and I want to add an extra column with extensionAttribute1. How do I go about that using import-csv / export-csv ?
Thanks
Hi there,
Do you know a way to populate multiple users from different OU with an extension attribute please ?
thanks,
François
I found my answer minutes ago, 🙂
If it can helps :
csv looks like :
Name,extensionAttribute5
t-gal,hopsuisse
then :
Import-Csv -Path “C:\temp\fr\testattribute.csv” |
ForEach-Object {
Set-ADUser $_.Name -add @{
ExtensionAttribute5 = “$($_.ExtensionAttribute5)”
}
thanks