Powershell for getting a CSV Adressbook out of an Active-Directory

This is a little reminder to myself on how-to get a CSV list out of an standard Active Directory/ldap:

[string] $ldapFilter = "(&(objectClass=User)(!(objectClass=Computer))(displayName=*, *)(telephonenumber=*))";
[string[]] $propLoad = @("displayname", "telephoneNumber");

[System.DirectoryServices.DirectoryEntry] $baseDn = New-Object "System.DirectoryServices.DirectoryEntry" "LDAP://ldap...";
[System.DirectoryServices.DirectorySearcher] $searcher = New-Object "System.DirectoryServices.DirectorySearcher" $baseDn, $ldapFilter, $propLoad, 2

$searcher.PageSize = [Int32]::MaxValue;

[System.DirectoryServices.SearchResult] $result = $null;

[string[]] $resultList = @();
foreach($result in $searcher.FindAll())
{
$resultList += $result.Properties["displayname"][0].ToString() + ";" + $result.Properties["telephonenumber"][0].ToString();
}

[System.IO.File]::WriteAllLines("C:\Temp\PhoneRF.CSV", $resultList, [System.Text.Encoding]::UTF8);


Beitrag veröffentlicht

in

, ,

von

Schlagwörter: