Powershell to get those accounts with mailNickName in "not set" state

Eaven HUANG 2,166 Reputation points
2021-07-16T11:40:51.81+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,521 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 54,316 Reputation points
    2021-07-16T14:04:02.433+00:00

    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.

    0 comments No comments

  2. 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
    
    0 comments No comments

  3. 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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.