Bruno Gomes
Bruno Gomes

Reputation: 1425

Wrapping all ADUsers in a CSV File

I want to retrieve, in a CSV file, all AD users. But every time I run my script the CSV file has a different amount of users.

I know there are up to 4000 users... but retrieved sometimes 500 to 600 results.

I noticed in my CSV file, at the last row something like

"Person Name", "[email protected]","person.name","CN=somewhere,OU=USERS,OU=THERE,OU=HERE,OU=SOMEPLACE,OU=ORG,DC= (here is the part where it breaks)

I noticed, always in the final row, the result is break. Is there a limit to my CSV file?

I can't figure out, what is happenning.

$path = ".\Users.csv"

$root = [adsi]''
$searcher = New-Object System.DirectoryServices.DirectorySearcher($root)
$searcher.Filter = "(&(objectClass=user))"
$searcher.PageSize = 5000 #iknow the max is 1000, but when i do it, and count my result, its show up 4000+
$searcher.SizeLimit = 5000


$cols = "cn", "mail", "samaccountname", "distinguishedname"
foreach ($i in $cols) {$searcher.PropertiesToLoad.Add($i)}

$users = $searcher.FindAll() |
  Select-Object @{e={$_.properties.cn};n='DisplayName'},
                @{e={$_.properties.mail};n='Email'},
                @{e={$_.properties.samaccountname};n='sAMAccountName'},
                @{e={$_.properties.distinguishedname};n='distinguishedname'}

$users | Export-Csv $path -Delimiter ";" -Encoding Default #now delimiting using ";" to do have problems with my string with commas

Upvotes: 2

Views: 370

Answers (2)

JPBlanc
JPBlanc

Reputation: 72680

In my opinion if you want to massively export data from an AD you can use integrated tools like LDIFDE.EXE to use LDIF format, if you want to export to CSV format you can use CSVDE.EXE.

csvde -f exportfile.csv -d "DC=SILOGIX-ESS01,DC=local" -r "(&
(objectClass=user))" -l DisplayName,Email,sAMAccountName,distinguishedname

CSVDE.EXE is a native Microsoft tool.

Upvotes: 2

Benjamin Hubbard
Benjamin Hubbard

Reputation: 2917

Use the ActiveDirectory module cmdlets. So much easier. Looks something like this:

$path = ".\Users.csv"
Get-ADUser -Filter * | 
  Select-Object cn, mail, samaccountname, distinguishedname |
  Export-Csv -Path $Path

Depending on your version of PowerShell, you may need to manually import the module.

Import-Module ActiveDirectory

Upvotes: 3

Related Questions