Powershell – AD – GetADUser filtered by certain properties

You may need to find a set of AD users filtered by certain identifying features.

Eg, “Can you please find me all users that belong to Company A, that either have a Title containing “Manager” or “Supervisor”, and are enabled in AD.

Notes

  1. The Active Directory Module will be required. You can install that by following this post.
  2. There are many properties that can be returned from an AD user object, to see all of them you can run “Get-ADUser $name -properties *”
  3. Extra parameters used with Export-CSV explained
    1. -NoTypeInformation = Do not include the type information at the top of the CSV

Code description

  1. The Get-ADUser commandlet will run, filtering against the conditions provided
  2. The results will be piped to select fields that are required
  3. All information will be piped to export CSV

Code

Get-ADUser -Filter {(Company -eq "CompanyA") -and ((Title -like "*Manager*") -or (Title -like "*Supervisor*")) -and (Enabled -eq "True")} -Properties * | select GivenName, Surname, SamAccountName, EmailAddress, Title, Company, Department, Country, st, Office, OfficePhone, MobilePhone, LastLogonDate, createTimeStamp, Enabled | Export-CSV "C:\temp\Export.csv" -Append -NoTypeInformation
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s