Link to home
Start Free TrialLog in
Avatar of Kelly Garcia
Kelly GarciaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.

Regards,
Kay
Avatar of FOX
FOX
Flag of United States of America image

Kay
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
ASKER CERTIFIED SOLUTION
Avatar of Jason Crawford
Jason Crawford
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Kay,
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.
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


Otherwise...
Has UPN
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