PowerShell help to create CSV with specific user data

npinfotech
npinfotech used Ask the Experts™
on
I am trying to create a .csv file with user data from a specific OU in Active directory.  

SamAccounName
First Name
Last Name
Full Name
Description
Email Address

Can someone please provide me with a powershell statement that will get this information into a CSV file?  This is what I have so far:

1. Open PowerShell, as an administrator, on a Domain Controller computer

2. Type in:

Get-ADUser -SearchBase "OU=Atlanta,OU=Georgia,DC=Company,DC=ORG" -prop * -Filter * | select SamAccounName,FirstName,LastName,FullName,Description,EmailAddress | export-csv c:\users\me\desktop\user-info-atlanta.csv

I am obviously new to PowerShell, so I want to be sure no data will be edited, only read =)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018
Commented:
The optimized solution will be this:

Get-ADUser -SearchBase "OU=Atlanta,OU=Georgia,DC=Company,DC=ORG" -Filter * -Properties EmailAddress,Description  | Select SamAccounName,GivenName,SN,Name,Description,EmailAddress | Export-Csv -NoTypeInformation  c:\users\me\desktop\user-info-atlanta.csv

Open in new window


By using -Prop * you are bringing 114 properties (for exch 2013) from those you are using just 2, so no need to ask for the 114 if you're using just 2, that's why the solution,

Givenname => firstname
Lastname => sn
Full Name => name

And at the end I added the -notypeinformation, here's the documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-6

Author

Commented:
Thank you, and thank you for making it more efficient.

You mention Microsoft Exchange above; will the statement work if I do not have Microsoft Exchange?  

Also, is there anything that needs to be typed in first (something like import-module), or can I start with the "Get-ADUser" command right away?
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Get-ADUser does not require Exchange. However, Exchange stores info in AD, and so you can get added info if installed.
PowerShell 3 and above do not require to implicitly import modules anymore, as long as the modules are in the paths set up for modules. The ActiveDirectory module is imported automatically with the first AD cmdlet you run in a session.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Thank you for the clarification, i really appreciate it.  The statement worked well.

The SN portion of the statement does not return the last name in the CSV file (last name for each user appears as a blank).  I right clicked on some of the users in "Active Directory users and computers", looked at the properties of some of the users and it did have the last name under SN.  Is there another way I can pull the last name?
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Try with SurName.

Author

Commented:
wonderful!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial