Powershell: List/Export Active Directory users UNDER certain OU incl. Home share
Searchbase = distinguishedName
How to find this out:
-
Start Active Directory User and Computers Console
-
Go to the OU you want to export and Right click > Attribute Editor
-
Copy the distinguishedName into the script below behind search base
-
Change your Domain controller behind Server
Searchbase = distinguishedName
Export all Active Directory attributes under certain OU Change all READ to your site info as mentioned above |
# Import the Active Directory module Import-Module ActiveDirectory # Define parameters for retrieving AD users $ADUserParams = @{ ‘Server’ = ‘yourdomaincontroller’ ‘Searchbase’ = ‘OU=User,OU=Schweiz,DC=butsch,DC=ch’ ‘Searchscope’ = ‘Subtree’ ‘Filter’ = ‘*’ ‘Properties’ = ‘*’ } # Define parameters for selecting specific user properties $SelectParams = @{ ‘Property’ = ‘SAMAccountname’, ‘CN’, ‘title’, ‘DisplayName’, ‘Description’, ‘EmailAddress’, ‘mobilephone’,@{name=’businesscategory’;expression={$_.businesscategory -join ‘; ‘}}, ‘office’, ‘officephone’, ‘state’, ‘streetaddress’, ‘city’, ’employeeID’, ‘Employeenumber’, ‘enabled’, ‘lockedout’, ‘lastlogondate’, ‘badpwdcount’, ‘passwordlastset’, ‘created’,’homeDrive’,’homeDirectory’ } # Get AD users with specified parameters and select specific properties Get-ADUser @ADUserParams | Select-Object @SelectParams | Export-Csv “c:\edv\users.csv” |
Save Powershell as c:\edv\dump.ps1
Logon on to Domain Controller
Start Powershell
Run .\dump.ps1 from c:\edv folder (Notice the .\ infront of dump.ps1)
You will get a COMMA Seperated list like this |
#TYPE Selected.Microsoft.ActiveDirectory.Management.ADUser “SAMAccountname”,”CN”,”title”,”DisplayName”,”Description”,”EmailAddress”,”mobilephone”,”businesscategory”,”office”,”officephone”,”state”,”streetaddress”,”city”,”employeeID”,”Employeenumber”,”enabled”,”lockedout”,”lastlogondate”,”badpwdcount”,”passwordlastset”,”created”,”homeDrive”,”homeDirectory” |