Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve only enabled users from the Active Directory

I'm trying to retrieve only enabled users in the AD. When I run this code line it returns the error. I tried using a filter as well to filter only enabled users for the requested info but it returns ALL users from every domain instead of just the single id.

Get-ADUser : A positional parameter cannot be found that accepts argument 'enabled -eq 'true''.

This is my code that is throwing the error.

Get-ADGroupMember -Identity 'Animal Shop A' | Get-ADUser -Filter '*' | Get-ADUser Where "enabled -eq 'true'" | Get-ADUser -Properties ('Mail')

This one returns ALL users from every domain

Get-ADGroupMember -Identity 'Animal Shop A' | Get-ADUser -Filter "enabled -eq'true'" | Get-ADUser -Properties ('Mail')

Is my syntax wrong on both of them? If I just want to return values from say "Animal shop A" and then "Animal Shop B"

like image 783
Celestialchippy Avatar asked Jun 20 '18 17:06

Celestialchippy


2 Answers

Get-ADUser -Filter * -Properties mail | Where { $_.Enabled -eq $True} | Select Name,samaccountname,mail

That will get all enabled users in your target domain that are enabled and display the name, username, and mail properties

like image 184
trebleCode Avatar answered Oct 03 '22 13:10

trebleCode


.. or a little bit shorter this way:

Get-ADUser -Filter 'enabled -eq $true' -Properties mail | 
    Select-Object -Property Name,samaccountname,mail

Besides this I would recommend to use a -SearchBase. That's less stressful for the AD. ;-)

like image 40
Olaf Avatar answered Oct 03 '22 14:10

Olaf