question

EavenHuang avatar image
0 Votes"
EavenHuang asked IanXue-MSFT answered

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?)

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

Try this:

 Get-ADObject -filter { mailNickname -notlike "*" } -SearchBase $OUpath -Properties |
   Select-Object samAccountName, mailNickName, mail, Name, DistinguishedName, UserPrincipalName |
     Export-Csv -NoType $ExportPath
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.