Get-ADUser for all domains | Export-CSV

This topic contains 7 replies, has 2 voices, and was last updated by  Jeff Taylor 2 months ago.

  • Author
    Posts
  • #72890

    Jeff Taylor
    Participant

    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

  • #72893

    Don Jones
    Keymaster

    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.

    • #72895

      Jeff Taylor
      Participant

      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?

  • #72896

    Don Jones
    Keymaster

    Well, that's basically why I wrote "Learn PowerShell Toolmaking in a Month of Lunches." 🙂 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.

    • #72898

      Jeff Taylor
      Participant

      Yes I read that book cover to cover and helped with all the typos, remember? 🙂 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.

  • #72899

    Don Jones
    Keymaster

    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!

    • #72901

      Jeff Taylor
      Participant

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

    • #73009

      Jeff Taylor
      Participant

      WOOHOO I love Functions, thanks Don!

You must be logged in to reply to this topic.