0

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.

3
  • 1
    Assign the output from FindOne() to a variable, then call RefreshCache("list","of","attributes","to","load") on it to fetch the given attributes from the directory - after that the Properties collection should have the requested properties Commented Dec 22, 2023 at 15:17
  • I added: $user = $search.FindOne() $user.RefreshCache() $user.Properties | ConvertTo-Csv And I get an error stating that RefreshCache() isn't a method for [System.DirectoryServices.SearchResults] Commented Dec 22, 2023 at 19:49
  • Adding GetDirectoryEntry() removes the error, but I get properties of the attributes instead. Commented Dec 22, 2023 at 19:57

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.