Extract all mail enabled groups with PowerShell and ADSI

You may also like...

8 Responses

  1. Atom Bomb says:

    Hi Lewis!!

    Great script. Works like a charm. I too am not a PS scripter, and am trying to figure out how to edit your script to send output to a file (*.CSV or *.TXT). I would normally just add a redirect but that isn’t working for me. I also tried piping through to the Export command, with no luck. Any suggestions for me?

    Thanks!!

  2. Lewis says:

    If you want to export the content, remove the Write-Host entries at the beginning of the respective lines and then you’ll be able to use the pipe command.

  3. Freezman says:

    Hi,
    to get the output to file you need to use write-output instead of write-host, you can modify the script as follows:

    $RootDN = [ADSI] ”
    $Searcher = New-Object System.DirectoryServices.DirectorySearcher($RootDN)

    $Searcher.Filter = “(&(objectClass=group)(mail=*))”

    $MeGroups = $searcher.FindAll()

    Write-Output $(“There are ” + $MeGroups.Count + ” mail enabled groups (Security & Distribution)”)

    ForEach ($Group in $MeGroups) {

    $GroupDN = [ADSI]$Group.Path
    Write-Output $(“Group: ” + $GroupDN.displayName + ” (” + $GroupDN.mail + “)”)

    ForEach ($Member in $GroupDN.member) {

    $Member = $Member.ToString()
    $Entry = $Member.Split(“,”)
    $Entry = $Entry[0] -replace(“CN=”,””)

    Write-Output $($Entry)

    }

    Write-Output $(” “)

    }

    save that script (for example DLs.ps1) and then in powershell run C:\Temp\DLs.ps1 | out-file c:\temp\AllMailEnabledGroups.txt

    Hope this helps.
    BR

  4. Eddie Dillard says:

    Great script but do you have a script that will list all the mail enabled distribution groups (including dynamic distribution groups) for an individual AD user? Ideally I”d liked to import list of users from .txt or .csv file and export the results to a .csv file.

  5. Lewis says:

    Hi Eddie, if you just need a list of the groups that a user is a member of, use a one-liner.

    Get-ADUser -Identity my.user -Properties memberOf | % {$_.memberOf} | % {Get-ADGroup $_ -Properties *} | select name, mail

    See how you get on with that as a starting point. Use Import-CSV and a ForEach.

    Cheers
    Lewis

  6. Adam says:

    perfect just what I was looking for Lewis!!!

  7. Freddie says:

    Good script, but I need to find out in a specific OU, which Groups are mail enabled? we are trying to clean up AD with empty groups and want to make sure none are mail enabled.

    Thanks

    Freddie

  8. joshua says:

    $RootDN = [ADSI] ”
    $Searcher = New-Object System.DirectoryServices.DirectorySearcher($RootDN)

    $Searcher.Filter = “(&(objectClass=group)(mail=*))”

    $MeGroups = $searcher.FindAll()

    Write-Host “There are” $MeGroups.Count “mail enabled groups (Security & Distribution)”

    ForEach ($Group in $MeGroups) {

    $GroupDN = [ADSI]$Group.Path
    Write-Host $GroupDN.displayName “(“$GroupDN.mail”)”

    ForEach ($Member in $GroupDN.member) {

    $Member = $Member.ToString()
    $Entry = $Member.Split(“,”)
    $Entry = $Entry[0] -replace(“CN=”,””)

    Write-Host `t $Entry

    }

    $Entry | Export-Csv -Encoding ‘Unicode’ c:\temp\ngtest.csv

    }
    the following script is making the file ngtest.csv but the output I am seeing is

    #TYPE System.String
    Length
    14
    in excel

Discover more from lewisroberts.com

Subscribe now to keep reading and get access to the full archive.

Continue reading