Link to home
Start Free TrialLog in
Avatar of skbarnard
skbarnard

asked on

Powershell syntax to export AD users

I'm trying to export all users in our AD structure using powershell.  I don't want to export the student OU (I work for a school district).  I only need to export their name (either using displayName or sn and givenName attributes)
I want to export this to a CSV file.  I've attached my attempts, the second one didn't throw any errors but didn't produce any output either.
The examples in the attachments were from an internet search but since neither of these helped, I'm reaching out to the experts that usually get me on the right track.
I'm relatively OK with powershell but the syntax usually chokes me up.  I was using powershell mostly for Exchange so switching to AD gives a little bit of a new learning curve.
As always,  thanks
Examples.txt
Avatar of becraig
becraig
Flag of United States of America image

Try this:
Get-ADUser -ResultPageSize 0  -SearchBase "DC=domain,DC=us" | select-Object DisplayName,sAMAccountname,sn,givenname | Export-Csv c:\Users\username\Desktop ADusers.csv

Open in new window

Avatar of skbarnard
skbarnard

ASKER

I'm running the command above - however, it errored out because there wasn't a -Filter in the command (actually it started running but was requiring input for the filter).  I'm using -Filter * but I believe that will include the student OU as well.  I see KB being added to the file so I know data is being exported but the command is still running so I don't want to bother opening the file.
Since I have to put the -Filter object in the command, would it work to use -Filter {!("District Students")}?
****Update*****
The command finished.  It's not filling in the displayName for staff or sn.  It's including computers and as I thought, students.  So my question above is still valid but now I need to ask --- how do I exclude computers as well?
Get-ADUser only returns users - it won't output computers.

Are all your users that you want in an OU branch that is separate from the Students branch?  Or put another way, if you specify a specific OU as a searchbase which will contain all your desired users, is that OU also a parent (or grandparent, etc.) of your students OU?  If not then the following should work.
Get-ADUser -Filter * -SearchBase "OU=notstudents,DC=domain,DC=us" -Properties DisplayName | Select-Object DisplayName,sAMAccountname,surname,givenname | Export-Csv c:\Users\username\Desktop ADusers.csv

Open in new window


Otherwise, we would have to filter through the various OUs one at a time in order to exclude the students' OU.
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This looks like it's going to get me on the right track - one other thing though, is there a way to add another OU to the exclusion?  I have an OU that is all our disabled accounts and I don't need to include those either.
I tried doing {$_.name -ne 'disabled accounts'} right after the other curly bracket entry and it errored out on me.
Yes you can update the line to read
{$_.name -ne 'District Students' -and $_.name -ne "other OU"}

Open in new window


etc... etc...
Sorry - I found the syntax to exclude multiple groups.  The command is currently running and I see data being added to the CSV file.  I'll report back in a little bit on whether this got me what  needed.
One last thing, I really would like to have the user's location be exported as well.  I just tried using the 'physicalDeliveryOfficeName' attribute.  That field didn't populate and what's really weird is students now are on the list.  What attribute do I need to use to show their office location?
If you want other attributes of the accounts, you need to specify them with the -properties parameter of Get-ADUser, and then also include it in the Select-Object command.
The command selected as the solution did the lion's share of the work for me but all the other commands in this thread contributed to me being able to export just the staff members from our AD structure.  Thanks to all who contributed