Search code examples
powershellsyntaxactive-directoryuser-managementuser-account-control

Get all users from active directory using filter


I'm trying to use below script to get all active users from AD, however, I don't get any results although I know there are data with value 512.

Do you know what I have wrong here?

  Get-ADUser -filter {$userAccountControl -eq "512"} -properties Name,userAccountControl -Server myserver.local | Export-CSV "E:\Folder\ADusers.csv" -NoTypeInformation -Encoding UTF8

Solution

  • As pointed out in the comments, the $ does not belong. That tells PowerShell that you want to use the value in a variable called $userAccountControl and compare that to 512. Considering that you probably never set a variable called $userAccountControl, that means it's comparing nothing to 512 and seeing that it's not true for every account.

    Remove the $ and it will compare the property called userAccountControl to 512.

    Get-ADUser -filter {userAccountControl -eq "512"} -properties Name,userAccountControl -Server myserver.local | Export-CSV "E:\Folder\ADusers.csv" -NoTypeInformation -Encoding UTF8