You are getting AD objects filtered where the mailNickname is an empty string. If the property isn't set then it'll get filtered out. The easiest solution is to remove the filter and then you'll get everything. But it seems like you should be filtering on other properties like just the users type but I assume your search base is handling that.
Powershell to get those accounts with mailNickName in "not set" state
Dear All,
I googled around today and use following script to get the account list from a specific OU where it contains many sub-OUs.
Get-ADObject -filter { mailNickname -like "*" } -SearchBase $OUpath -Properties * |
select samAccountName, mailNickName, mail, Name, DistinguishedName, UserPrincipalName |
Export-Csv -NoType $ExportPath
However, I found in the .csv that those users with mailNickName as "not set" status didn't get listed in the file. What I was trying to do is to list all the users in the OU with their respective attributes like mail, mailNickName, samAccountName, etc.
What am I missing? (There were star chars in the script but somehow they were not seen?)
3 answers
Sort by: Most helpful
-
-
Rich Matheisen 46,711 Reputation points
2021-07-16T14:51:41.087+00:00 Try this:
Get-ADObject -filter { mailNickname -notlike "*" } -SearchBase $OUpath -Properties | Select-Object samAccountName, mailNickName, mail, Name, DistinguishedName, UserPrincipalName | Export-Csv -NoType $ExportPath
-
Ian Xue 36,751 Reputation points Microsoft Vendor
2021-07-19T09:47:13.623+00:00 Hi,
Please try the Get-ADUser cmdlet with the filter condition removed.
Get-ADuser -filter * -SearchBase $OUpath -Properties mailNickName, mail | select samAccountName, mailNickName, mail, Name, DistinguishedName, UserPrincipalName | Export-Csv -NoType $ExportPath
Best Regards,
Ian Xue============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.