Powershell script to make csv file with active directory users

My boss asked me to create a csv file with all the users in one container on AD.  Since I'm still learning powershell and we need this right away, I'm asking you guys.  I spent a few hours looking at example code, but I'd like something that works.

I need to collect the name and email address from the container Alpha in my domain.  This needs to be outputed to a csv.

Not that complicated.
LVL 3
wiglackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Will SzymkowskiSenior Solution ArchitectCommented:
Pretty strightforward, use the below PS command...

Get-Aduser -filter * -searchbase "OU=Alpha,DC=domain,DC=com" -properties * | select Displayname, Mail | export-csv c:\users.csv

Note: if  Alpha is an actual container rather than and OU you would use CN= in stead of OU=.

Thanks

Will.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wiglackAuthor Commented:
Thank you.  That works perfectly for the Users container.  

I am also required to retrieve from another container full of contact objects.  This command doesn't seem to work with contacts.  How would I accomplish this?
0
Will SzymkowskiSenior Solution ArchitectCommented:
Use the following command...
get-contact -Filter 'name -like "*"' -OrganizationalUnit "OU=Alpha,DC=yourdomain,DC=com" | select Name, WindowsEmailAddress | export-csv "c:\contacts.csv"

Note: if you want to specify an OU it needs to be an OU not a container. If you want to remove the -Organizational parameter this will work but it will search for all contacts in your domain instead.

Thanks

Will.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

wiglackAuthor Commented:
The contacts are in a container.  Also, This cmdlet is not recgonized.  Do I have to import a certain module to make that cmdlet work?

cn=emergencycontacts,dc=domain,dc=com
0
SubsunCommented:
Get-Contact is a Exchange Management shell (EMS) command, you need to run it from EMS or you need to add the exchange pssnapin to console before you run this command
0
Will SzymkowskiSenior Solution ArchitectCommented:
The get-contact command will not work with a container. You need to use an OU for this. Why not just create a new OU called "emergencycontacts" and move all of the contacts from the container to the OU?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.