Accounts with UPN, Powershell

HI All,

I have a long list of users accouts, contacts, etc. I need to check which ones have UPN and which ones do not and then output them into an excel.

I have written this code, however it stops. I think i need to filter out the contacts. Please help!

Get-Content .\userid2.txt  | % { Get-QADUser $_ | ? {$_.UserPrincipalName -notlike $null} } > noupn2.txt

Get-Content .\userid.txt  | % { Get-QADUser $_ | ? {$_.UserPrincipalName -like $null} } > noupn.txt

I was then going to copy and paste the results to excel.

Kelly GarciaSenior Systems AdministratorAsked:
Who is Participating?

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

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.

FOXActive Directory/Exchange EngineerCommented:
I have something easier for you.  Put all the users in one list and name it users.txt
1. Open powershell with (run as administrator)
2. Type    import-module activedirectory
3. Copy and past this command  
Get-content C:\pathtoyourfile\users.txt  Foreach-object {
Get-ADuser -filter * -property Userprincipalname | select samaccountname,userprincipalname | sort userprincipalname,samaccountname |out-file c:\pathtoyour\upnlist.txt }

**edit the command to match your paths**
This will give you a complete list of users with their upn and users who have blank upns
Jason CrawfordTransport NinjaCommented:
The original poster indicated his list included contacts which cannot have a userPrincipalName.  We also don't know what format the list uses to reference the users (ie First Last, samAccountName, email address, etc).  Please indicate how the users are listed.  What types of AD objects are represented in this list other than Users and Contacts?  I'm assuming there's others based on the etc.

Please don't run the command the previous poster included in step 3.  It won't work.  Get me more info and I'll give you a one-liner you can use.  Also, what is the ultimate goal here?  It's a little odd you're running a report for $null values, and I'm thinking maybe there's a better way to accomplish your goal.

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
FOXActive Directory/Exchange EngineerCommented:
I did overlook the fact that you did say you have contacts in your list as well. However my command will in fact work for a list of users.
Jason CrawfordTransport NinjaCommented:
Not with the missing pipe in between Get-Content and ForEach-Object it won't.
Why not just dump all users into a CSV and then just use Excel to filter as to whether or not they have a UPN?
gc userid.txt | Get-ADUser | Select Name, Userprincipalname | export-csv c:\allusersupn.csv

Open in new window

gc userid.txt | Get-ADUser -Filter {Userprincipalname -like "*"} | export-csv c:\hasupn.csv

Open in new window

Has no UPN
gc userid.txt | Get-ADUser -Filter {Userprincipalname -notlike "*"} | export-csv c:\noupn.csv

Open in new window

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

From novice to tech pro — start learning today.