Search code examples
powershellactive-directory

Export all AD User attributes to CSV without RSAT


I'm kludging my way to learning PowerShell and I've hit a solid wall.

Problem: I cannot figure out how to export user attributes to spreadsheet/csv correctly. I also do not have access to RSAT or Get-ADUser.

End Goal: Compare attributes and data formatting of the same user on two different Active Directory domains.

Current code:

$search = [ADSIsearcher] "(sAMAccountName=SampleUser*)"
$search.SearchRoot = [ADSI]"LDAP://Office.Domain.Example"
$search.FindOne().properties | Format-Table -Wrap #Exact what I want, but in file shape

#Exports attempted
$search.FindOne().properties | Export-CSV -NoTypeInformation "SampleUser.csv" 
#Just gives properties of the attributes

New-Object PSObject -property $search.FindOne().properties | Export-CSV -NoTypeInformation "SampleUser.csv" 
#I get the attribute names, but no values, only "ResultPropertyValueCollection", also its two rows, instead of two columns.

I'm definitely out of my depth, and I think my biggest knowledge gap is how PowerShell objects work. While I can make a table just fine, parsing that into a format that I can easily and manually compare it is a pain.

Would someone kindly point out my main mistake and point me in the right direction?

Additional context: I'm trying to build a GUI search tool to find employees across our multiple AD domains. Outlook's Address Book is terribly slow and the options extremely limited. Also, it doesn't connect to the other domains. Ex: employee ID is extremely easy to get, but can't be used to find someone in Address Book.


Solution

  • This works, but I'm pretty sure the code is going through more steps than necessary.

    $search = [ADSIsearcher] "(sAMAccountName=SampleUser*)"
    $search.SearchRoot = [ADSI]"LDAP://Office.Domain.Example"
    $table = @{}
    $user.Properties.GetEnumerator() | ForEach-Object {$table += @{$_.Key = $_.Value -join ';|;' }}
    $table.GetEnumerator() | Export-Csv -NoTypeInformation "SampleUser.csv"