Sunday, 26 October 2014

PowerShell: Get-ADUser - Filter and Select Attributes

The Active Directory powershell cmdlet Get-ADUser supports different default and extended properties. Refer this article Get-ADUser Default and Extended Properties for more details. Get-ADUser cmdlet also supports smart LDAP Filter and SQL Like Filter to select only required users. In this article, I am going to write different examples to list AD user properties and Export AD User properties to CSV using PowerShell.

Get-ADUser - Select all properties:

Use the below code to list all the supported AD user properties.
Import-Module ActiveDirectory
Get-ADuser -identity 'Morgan' -Properties *          

Get-ADUser - Filter and List Selected properties:

This command lists the selected properties as table format of AD users whose City contains the text 'Austin'.
Import-Module ActiveDirectory
Get-ADUser -Filter 'City -like "*Austin*"' -Properties * |
 Select -Property Name,City,Mail,Department | FT -A          

Get-ADUser - LDAP Filter:

Instead of SQL Like Filter, you can also use LDAP filter to select only required users. Refer this article (AD LDAP Filter Examples) to get more LDAP filter examples.
Import-Module ActiveDirectory
Get-ADUser -LDAPFilter '(Department=*Admin*)' -Properties * |
  Select -Property Name,City,Mail,Department,DistinguishedName | FT -A 

Get-ADUser - Select users from specific OU:

This command select all the AD users from the Organisation Unit 'Austin' and lists the selected properties.
Import-Module ActiveDirectory
Get-ADUser -Filter * -SearchBase "OU=Austin,DC=TestDomain,DC=Local" -Properties * |
 Select -Property Name,Mail,Department | FL           

Get-ADUser - Export Selected properties to CSV file:

This command export the selected proprties to CSV file of AD users whose City contains the text 'Austin'.
Import-Module ActiveDirectory
Get-ADUser -Filter 'City -like "*Austin*"' -Properties * |
  Select -Property Name,City,Mail,Department,DistinguishedName | 
  Export-CSV C:\\ADUsers.csv -NoTypeInformation -Encoding UTF8
Export ADUsers CSV output:
AD PowerShell: Get-ADUser - Export Selected properties to CSV file

Advertisements
Advertisements

No comments:

Post a Comment