Solved

Powershell syntax to export AD users

Posted on 2014-09-11
11
245 Views
Last Modified: 2014-09-16
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
0
Comment
Question by:skbarnard
  • 6
  • 3
  • 2
11 Comments
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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

0
 

Author Comment

by:skbarnard
Comment Utility
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")}?
0
 

Author Comment

by:skbarnard
Comment Utility
****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?
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
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.
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
Comment Utility
...and that would be something like
$OUs = Get-ADOrganizationalUnit -Filter * | ? {$_.name -ne 'District Students'} | Select -expand DistinguishedName
$OUs | ForEach { Get-Aduser -filter * -searchBase $_ -SearchScope OneLevel -Properties DisplayName } | Select-Object DisplayName,sAMAccountname,surname,givenname | Export-Csv c:\Users\username\Desktop ADusers.csv

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:skbarnard
Comment Utility
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.
0
 
LVL 28

Expert Comment

by:becraig
Comment Utility
Yes you can update the line to read
{$_.name -ne 'District Students' -and $_.name -ne "other OU"}

Open in new window


etc... etc...
0
 

Author Comment

by:skbarnard
Comment Utility
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.
0
 

Author Comment

by:skbarnard
Comment Utility
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?
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
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.
0
 

Author Closing Comment

by:skbarnard
Comment Utility
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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now