Get-ADUser for all domains | Export-CSV

I’m searching for all Users who have a value in the msExchRecipientDisplayType attribute, in all (5) domains.

I’m only able to export results for one of the domains (I assume it’s the last one in the pipeline) and not sure why:

foreach ($domain in (get-adforest).domains) { get-aduser -filter {msExchRecipientDisplayType -like "*" }`
    -server $domain -Credential $creds -Properties extensionAttribute5,msExchRecipientDisplayType | sort samAccountName,DistinguishedName |
        Select-Object samAccountName,DistinguishedName,extensionAttribute5,msExchRecipientDisplayType |
            Export-Csv .\output.csv -NoTypeInformation }

Thank you

Well this would probably be better written as a function. But, your ForEach construct isn’t part of a pipeline. It’s a scripting statement. It’s actually exporting all of them - it’s just, each time, it overwrites the previous file, so to you it only looks like the last one worked.

as long as I don’t use the export-csv cmdlet (and use ft instead) I get to see all the domains with this one liner:

               foreach ($domain in (get-adforest).domains) { get-aduser -filter {msExchRecipientDisplayType -like "*" }`
    -server $domain -Credential $creds -Properties extensionAttribute5,msExchRecipientDisplayType | sort samAccountName,DistinguishedName |
        Select-Object samAccountName,DistinguishedName,extensionAttribute5,msExchRecipientDisplayType |
                ft samAccountName,DistinguishedName,extensionAttribute5,msExchRecipientDisplayType -autosize }

Can you show me a better way using a Function then, to capture all my domains into a .csv?

Well, that’s basically why I wrote “Learn PowerShell Toolmaking in a Month of Lunches.” :slight_smile: Which I heartily recommend if you want to learn to do this the right way with a lot less work.

Function Get-MyStuff {

do your stuff here - don’t export it to CSV, just let it output to the pipeline

basically put your code here but delete the export-CSV and the pipe right before it

}

Get-MyStuff | Export-CSV filename.csv

Your code is working just fine. It’s just that each domain results in a new CSV file, which happens to be the same as the previous CSV file name, so it just overwrites it.

Yes I read that book cover to cover and helped with all the typos, remember? :slight_smile: and then bought all of Manning Presses books too. I’ll get there I just have to reconcile actual work with text book examples and find it takes time. I appreciate your patience and I do search to forum first for my elementary questions but most are quite advanced still for me.

That little template I posted above should do what you need - give that a shot as a starting point. I mainly want to make sure you know why your existing code isn’t working - so if that’s not clear, let me know.

Like, if you used a variable of some kind instead of output.csv for the filename, and you changed that variable each time through, you’d get multiple CSV files with everything you want. You’re that close. Format-list works because it doesn’t “overwrite the existing file” like Export-CSV does.

Heck, just add -Append to your Export-CSV command and you’ll get something closer to what you want. It’s more important to me that you understand why, though - so please, ask if it’s not clear!

Yep, I do understand what’s happening. Will play with and learn from the output(s). Thanks again for the various options.

WOOHOO I love Functions, thanks Don!