Asking for PowerShell script to export list of Distribution Groups with members and email addresses in an OU to a .csv file

donander
donander used Ask the Experts™
on
I want to get a .csv file listing the members (and email addresses) of all the distribution groups in one OU in Active Directory. I have found code that is supposed to do this but when I run it in the PowerShell command window I get this error:

Get-DistributionGroup : The term 'Get-DistributionGroup' is not recognized as the name of a cmdlet, function, script file, or operable program.

The forum where I got that is from 2012 so has that command been deprecated?

Bottom line is, can someone provide this script?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
hello ,

normally this command should work on exchange server

you can use this to find command in powershell

Get-Help *distrib*


on your AD you can try this

Import-Module ActiveDirectory

Get-ADGroup -Filter {GroupCategory -eq "Distribution"} -Properties Members

Thanks and Regards,

Author

Commented:
When I run it I get the following result which I don't think is what I need. Strange.

PS H:\> Get-Help *distrib*

NAME
    Enable-BCDistributed

SYNTAX
    Enable-BCDistributed


ALIASES
    None


REMARKS
    None

Author

Commented:
In the meantime, I have received the names of the Distribution Groups so I tried this:

Import-Module ActiveDirectory
Get-DistributionGroupMember -identity "!Dept_MyDepartment_Faculty" | export-csv -NoTypeInformation -path.\Dept_MyDepartment_Faculty.csv

but I still get the same error about the command not being recognized.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
I ran

PS H:\> Get-Help Get-

and got hundreds of results. I copied them into MS Word, sorted them alphabetically and searched for

get-dis and there are only the 3 shown below with the result before and after so it appears that my PowerShell doesn't know about Get-Distribution...

Get-DhcpServerVersion             Function  DhcpServer                ...
Get-Disk                          Function  Storage                   ...
Get-DiskImage                     Function  Storage                   ...
Get-DiskStorageNodeView           Function  Storage                   ...
Get-DnsClient                     Function  DnsClient                 ...

Commented:
Hello,

if you can't find the command in powershell, you can't use it

try this

Import-Module ActiveDirectory

Get-ADGroup -Properties Members -Filter {identity -notlike "Dept_MyDepartment_Faculty"}


command Get-ADGroup -Properties Members give you the same output that  Get-DistributionGroupMember command will do.

you are not on exchange server so i don't think you will find the command  Get-DistributionGroupMember
Jacob DurhamIT Support Analyst II (Lead Infrastructure Engineer)

Commented:
You need to either run this on the DC or install the Active Directory Remote Server Administration Tools to your workstation.
FOXActive Directory/Exchange Engineer
Top Expert 2015

Commented:
Donander,
You have stated that you have the names of the distribution groups.  So let's take it from there
Open Exchange Management Shell
1. On the machine where you have the shell open Create a folder on your c:\drive named Temp
2.  I want you to list all the groups one underneath each other with a header of Name(example below)
Name
GroupA
GroupB
DistrGroupC

Whether the file is made in notepad or excel please name the file Grps.csv and save it in the Temp folder on your c: drive
So your file should be c:\temp\Grps.csv

Now Create a folder inside of Temp named DLs
example c:\Temp\DLs     (inside Dls is where all your new .csv files will drop with the info you need after you run the below command)

Now for the magic:

Import-csv 'c:\temp\grps.csv' | %{ $name = $_.Name; Get-DistributionGroupMember $_.Name | Select Name,PrimarySmtpAddress | Sort Name | Export-csv "c:\temp\DLs\$name.csv" -notypeinformation }

Author

Commented:
Sorry, I didn't see these comments until just now.

Regarding comment from spmt:
If I execute
Get-ADGroup -Properties Members -Filter {identity -notlike "Dept_MyDepartment_Faculty"}
substituting the actual group name, the output is blank (just returns to the prompt)

Regarding comment from Jacob Durham:
I have RSAT and ran "Active Directory Module for Windows PowerShell" and pasted the script in that window but still get the same error.

Regarding comment by FOX:
I don't have access to Exchange Management Shell. I work for one of the departments at our organization, not for the central IT team and I don't have permissions to manage Exchange.

Commented:
Hello,

sorry i have make a mistake

good one is :

Get-ADGroup -Filter {GroupCategory -eq "Distribution" -or Identity -notlike "Dept_MyDepartment_Faculty"} -Properties Members

or play with filter to get what you want.


do this on your AD or install Active Directory Remote Server Administration Tools to your workstation.

even if you install Active Directory Remote Server Administration Tools to your workstation , you will not have Exchange command so  you cannot do Get-DistributionGroup expect if you install remote tools for exchange


Thanks and Regards

Author

Commented:
spmt,

When I run this and substitute the actual name of the distribution group for "Dept_MyDepartment_Faculty" I get all the distribution groups in AD EXCEPT that group. I think -notlike should not be in there. What I want is to just get the names and email addresses in a .csv file for a distribution group whose name I supply in the script.
Thanks,
Don
Active Directory/Exchange Engineer
Top Expert 2015
Commented:
Donander,
Test this on one group    (please create a folder on your c:drive named Temp)

Get-ADGroupMember 'GroupName' -Recursive | Get-ADUser -Properties Mail | Select-Object Name,Mail | Export-CSV -Path 'C:\temp\GroupName.csv' -NoTypeInformation

Author

Commented:
Code submitted by FOX does exactly what I want.
Sorry for the delay in testing.
Thanks!
Don

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial