Script to get active DL's not producing correct results RRS feed

  • Question

  • Hi everyone,

    I'm hoping someone can point me in the correct direction.  I'm trying to leverage a script that will report DL's that have not been used in the past 30 days.  See below

    Write-Output "Getting list of distribution Groups "

    $O365Groups = Get-DistributionGroup -anr Global

    Write-Output "Getting Distribution Group Usage"

    $O365GroupsInUse = $O365Groups | select -ExpandProperty primarysmtpaddress  | Foreach-Object { Get-messagetrace -startdate (get-date).adddays(-30) -enddate (get-date)-RecipientAddress $_  -pagesize 1| select -first 1}



    $Results = Foreach ($O365Group in $O365Groups) {



                if ($O365Group.primarysmtpaddress -in $O365GroupsInUse.RecipientAddress)


                     [pscustomobject]@{Name = $O365Group.Name ; Active = "Yes" ; Email = $O365Group.primarysmtpaddress }


                else {

                     [pscustomobject]@{Name = $O365Group.Name; Active="No" }





    $Results | Export-Csv c:\scripts\Office365Report.csv -nti

    When I run the script it comes back as all DL's not in use.  I know that's incorrect.  When I run it against one specific DL it always comes back as in use (even if it isn't).  When I add more than one it comes back as no on every DL.

    What am I missing here?

    • Edited by Scott(WW) Friday, November 30, 2018 4:51 PM no color
    • Moved by Bill_Stewart Friday, March 15, 2019 12:36 AM This is not "scripts on demand"
    Friday, November 30, 2018 3:13 PM

All replies

  • Please do not post colorized code.  Use the code posting tool provided.  Colorized code cannot be copied and is very hard to read in most browsers.


    Friday, November 30, 2018 4:16 PM
  • Sorry.  wasn't aware.  Anyways I was able to figure it out.  Simply changed the -match operator to the -in operator
    • Edited by Scott(WW) Friday, November 30, 2018 4:52 PM
    Friday, November 30, 2018 4:47 PM
  • You might be better off getting the message tracking logs from your O365 tenant and keep a rotating 30-day set of files. From those files you'll want to extract the "EXPAND" events and then get the DL name from those. Sort them and then use Get-Unique. That'll get you a list of all the DLs that have been used, even those that have no membership.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, November 30, 2018 8:17 PM