I need to write a Powershell script to retrive active directory user data into a spreadsheet

We have a Windows 2012 domain and need to find out which users in a specific OU have the Telephone number and E-mail fields completed and which are blank. Can someone show me an example? For example I have a Domain called USA.local and a OU TEXAS\DALLAS\DALLASUSERS.  I want to pull all display name, telephone number and email address of every active directory user account under the DALLASUSERS OU. All suggestions welcome.
LVL 1
Thor2923Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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:
Use the following script below...
Import-module activedirectory
Get-ADUser -Filter * -SearchBased "ou=dallasusers,ou=dallas,ou=texas,dc=youdomain,dc=com" -Properties DisplayName, Mail, TelephoneNumber |
? { ($_.TelephoneNumber -ne $null) -and ($_.Mail -ne $null) } | select DisplayName, mail, telephonenumber |
Export-csv "c:\results.csv" -nti 

Open in new window


Thats it.

Will.

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
systechadminConsultantCommented:
Yes you can do it using powershell. Refer the technel script which you can modify as per your requirement

https://gallery.technet.microsoft.com/scriptcenter/Get-list-of-AD-users-in-an-923fd124
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Just to correct typing mistake in Will's solution:
it was -searchbased   so removed 'd' and the new script will be :

Import-module activedirectory
Get-ADUser -Filter * -SearchBase "ou=dallasusers,ou=dallas,ou=texas,dc=youdomain,dc=com" -Properties DisplayName, Mail, TelephoneNumber |
? { ($_.TelephoneNumber -ne $null) -and ($_.Mail -ne $null) } | select DisplayName, mail, telephonenumber |
Export-csv "c:\results.csv" -nti 

Open in new window

Check Out How Miercom Evaluates Wi-Fi Security!

It's not just about Wi-Fi connectivity anymore. A wireless security breach can cost your business large amounts of time, trouble, and expense. Plus, hear first-hand from Miercom on how WatchGuard's Wi-Fi security stacks up against the competition plus a LIVE demo!

Thor2923Author Commented:
THANKS! I have a typo somewhere tho, it does not like something in my searchbase. I am assuming
dc=youdomain,dc=com would translate into dc=usa,dc=local   in my case??
Thor2923Author Commented:
ok I just found the typo, thanks Raheman. I think I still have some typos of my own also. I am working through them
Thor2923Author Commented:
OK IT WORKS!!!  I just have one question. My results show everyone has an email address and telephone number added to their active directory account. If someone happened to have a blank field would this script of still shown their display name with blanks in the mail or telephonenumber column? Right now it looks like all my users are perfect, I just want to make sure I am not missing something and incomplete users were bypassed....
Will SzymkowskiSenior Solution ArchitectCommented:
Change the -and to -or in the script on line 3.

@Raheman - good catch, typing too fast.

Will.
Thor2923Author Commented:
Wow you guys ROCK!  That worked....now that I have a CSV, I don't suppose there is an easy way for me to populate the empty fields with data and use a script to pull them back into active directory is there??
Thor2923Author Commented:
yea my next step looks more complicated and I need to be careful modifying AD accounts. I will post another question about that...thanks for all your help
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
Powershell

From novice to tech pro — start learning today.