locked
Powershell Script to export name and email of Distribution/Security groups in specified OU RRS feed

  • Question

  • Hi there,

    Im looking for a powershell cmd that will allow me to export a list of Distribution groups in a specified OU.

    The export needs to show Group name and email address. Im halfway there with the below cmd which exports the group name but doesn't do the email address, i understand it probably fairly simple to add the email to the cmd but my knowledge of Powershell scripting is next to none.

    Current cmd: get-adgroup -Filter * -SearchBase "OU=‎Distribution, OU Domain, etc, etc" | Export-Csv -NoType "DistributionGroups2.csv"

    I've searched the forums but couldnt find exactly what was looking for, if someone could kindly edit the above script to include email addresses that would be greatly appreciated.

    Thanks

    Tuesday, July 2, 2019 8:54 AM

Answers

  • The cmdlets Get-ADGroup or Get-ADUser or Get-ADComputer output by default only a subset of the possible properties. If you like to change that behaviour you have to provide the parameter -Properties with the attributes you need.

    With Select-Object you can limit the output to the properties you want. ... like this:

    Get-ADGroup -Filter * -SearchBase "OU=‎Distribution, OU Domain, etc, etc" -Properties mail | 
        Select-Object -Property Name,mail |
            Export-Csv -Path 'DistributionGroups2.csv' -NoTypeInformation


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, July 2, 2019 9:07 AM

All replies

  • The cmdlets Get-ADGroup or Get-ADUser or Get-ADComputer output by default only a subset of the possible properties. If you like to change that behaviour you have to provide the parameter -Properties with the attributes you need.

    With Select-Object you can limit the output to the properties you want. ... like this:

    Get-ADGroup -Filter * -SearchBase "OU=‎Distribution, OU Domain, etc, etc" -Properties mail | 
        Select-Object -Property Name,mail |
            Export-Csv -Path 'DistributionGroups2.csv' -NoTypeInformation


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, July 2, 2019 9:07 AM
  • Get-ADGroup will fetch all the groups from the OU including the security groups and others. If you need only the distribution lists specifically you can run the below command from any of the exchange management shell:

    Get-DistributionGroup -OrganizationalUnit "DOMAIN/OU" | Select Name, PrimarySMTPAddress | Export-Csv -Path .\Something.csv

    You should run the above cmdlet from any of the exchange shell or if you are using windows powershell please import the PSSnaping for exchange using Add-PSSnapin *exchange*. Also if the number of DLs are more than 1000 set the -Resultsize to unlimited or some other value as by default it will only fetch out the 1000 results.

    Tuesday, July 2, 2019 9:50 AM
  • Thank you for this, appreciate the speedy response.

    I did try and run it however it just sat there and no export appeared, there were no errors either. Any ideas?

    Wednesday, July 3, 2019 7:03 AM
  • Hi DumbleD0re,

    Thank you for your response, we dont dont actually have exchange enabled, only AD so i belibe Get-DistributionFroup may not work, but thank you anyway!!

    Wednesday, July 3, 2019 7:04 AM
  • Thank you for this, appreciate the speedy response.

    I did try and run it however it just sat there and no export appeared, there were no errors either. Any ideas?

    I take that back, i put the cmd on one line and it worked! Thank you!!!
    Wednesday, July 3, 2019 7:24 AM