none
Get-DistributionGroup with managers and full name RRS feed

  • Question

  • Hello,

    Our usernames in our AD are employee numbers. I am trying to get a report of all Distribution Group Owners with their FULL NAME. Not the employee number.

    I am trying to use this script but it keeps erroring. Any ideas on how to do this? I need a simple Excel report. DistGroup name, and Owner Full Name. That is all

    [PS] C:\Windows\system32>get-distributiongroup | ForEach-Object {$gname = $_.Name, $Manager = Get-AdUser $_.ManagedBy, $
    ManagerName = $manager.DisplayName}
    ForEach-Object : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADUser' required by
     parameter 'Identity'. Specified method is not supported.
    At line:1 char:39
    + get-distributiongroup | ForEach-Object <<<<  {$gname = $_.Name, $Manager = Get-AdUser $_.ManagedBy, $ManagerName = $m
    anager.DisplayName}
        + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ForEachObjectCommand

    [PS] C:\Windows\system32>

    Monday, January 11, 2016 5:02 PM

Answers

  • Actually it is a bit easier than I thought. The Translate-ADName.ps1 script isn't needed. Try it this way:


    Get-DistributionGroup | Where-Object { $_.ManagedBy } | ForEach-Object {
      $managedBy = $_.ManagedBy
      foreach ( $managerId in $managedBy ) {
        New-Object PSObject -Property @{
          "Group" = $_.Name
          "Manager" = $managerId.Name
        } | Select-Object Group,Manager
      }
    }
    

    (I'm not as familiar with the object types used by the Exchange cmdlets.)


    -- Bill Stewart [Bill_Stewart]



    Monday, January 11, 2016 7:05 PM
    Moderator

All replies

  • Hello,

    Our usernames in our AD are employee numbers. I am trying to get a report of all Distribution Group Owners with their FULL NAME. Not the employee number.

    I am trying to use this script but it keeps erroring. Any ideas on how to do this? I need a simple Excel report. DistGroup name, and Owner Full Name. That is all

    [PS] C:\Windows\system32>get-distributiongroup | ForEach-Object {$gname = $_.Name, $Manager = Get-AdUser $_.ManagedBy, $
    ManagerName = $manager.DisplayName}
    ForEach-Object : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADUser' required by
     parameter 'Identity'. Specified method is not supported.
    At line:1 char:39
    + get-distributiongroup | ForEach-Object <<<<  {$gname = $_.Name, $Manager = Get-AdUser $_.ManagedBy, $ManagerName = $m
    anager.DisplayName}
        + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ForEachObjectCommand
    • Merged by Niko.Cheng Tuesday, January 12, 2016 6:38 AM duplicate case!
    Monday, January 11, 2016 5:03 PM
  • Groups can have 0, 1, or multiple distinguished names (DNs) in the managedBy attribute.

    How do you propose to account for that?


    -- Bill Stewart [Bill_Stewart]

    Monday, January 11, 2016 5:38 PM
    Moderator
  • If there a 0 owners, we dont care. If there are more than 1 just make another line in the CSV file for each owner.
    Monday, January 11, 2016 5:40 PM
  • Start by trying to understand what happens with these CmdLets.

    Get-DistributionGroup |
         Get-DistributionGroupMember|
         select name,manager


    \_(ツ)_/


    • Edited by jrv Monday, January 11, 2016 5:45 PM
    Monday, January 11, 2016 5:45 PM
  • It spits out the Dist Group Name and members I think. I dont care anything about the members. I simply want a list of all Distribution Group with there ManagedBy as Full AD Display Name
    Monday, January 11, 2016 5:48 PM
  • Get-DistributionGroup | select name, ManagedBy

    \_(ツ)_/

    Monday, January 11, 2016 5:52 PM
  • That works but shows me their employee number (AD Username) I want the full display name. i.e. John Doe. not e177663
    Monday, January 11, 2016 5:53 PM
  • Use the AD cmdlets. I would suggest output a seperate object for each managedBy.


    $params = @{
      "LDAPFilter" = "(&(msExchRecipientDisplayType:1.2.840.113556.1.4.803:=1)(managedBy=*))"
      "Properties" = "managedBy"
    }
    Get-ADGroup @params | ForEach-Object {
      $managedBy = $_.managedBy
      foreach ( $managerDN in $managedBy ) {
        New-Object PSObject -Property @{
          "Group" = $_.DistinguishedName
          "managedBy" = $managerDN
        } | Select-Object Group,managedBy
      }
    }
    


    -- Bill Stewart [Bill_Stewart]

    Monday, January 11, 2016 5:59 PM
    Moderator
  • This gets you the name.

    Get-DistributionGroup | %{
    		$_.managedby | %{$_.DistinguishedName}
    	}


    \_(ツ)_/



    • Edited by jrv Monday, January 11, 2016 6:05 PM
    Monday, January 11, 2016 6:00 PM
  • This gets you the name.

    Get-DistributionGroup | %{
    		$_.managedby %{$_.DistinguishedName}
    	}


    \_(ツ)_/


    I get this:

     : Method invocation failed because [Microsoft.Exchange.Data.Directory.ADMultiValuedProperty`1[[Microsoft.Exchange.Dat
    .Directory.ADObjectId, Microsoft.Exchange.Data.Directory, Version=14.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad
    64e35]]] doesn't contain a method named 'op_Modulus'.
    t C:\reports\test.ps1:1 char:26
     Get-DistributionGroup | % <<<< {
       + CategoryInfo          : InvalidOperation: (op_Modulus:String) [ForEach-Object], RuntimeException
       + FullyQualifiedErrorId : MethodNotFound,Microsoft.PowerShell.Commands.ForEachObjectCommand

    Monday, January 11, 2016 6:04 PM
  • Use the AD cmdlets. I would suggest output a seperate object for each managedBy.


    $params = @{
      "LDAPFilter" = "(&(msExchRecipientDisplayType:1.2.840.113556.1.4.803:=1)(managedBy=*))"
      "Properties" = "managedBy"
    }
    Get-ADGroup @params | ForEach-Object {
      $managedBy = $_.managedBy
      foreach ( $managerDN in $managedBy ) {
        New-Object PSObject -Property @{
          "Group" = $_.DistinguishedName
          "managedBy" = $managerDN
        } | Select-Object Group,managedBy
      }
    }
    


    -- Bill Stewart [Bill_Stewart]

    The script ran but it did not give me full display name. It same me the employee number(AD Username) again. This look like the ticket if we can just get correct Display Name now.
    • Edited by WREED_CISCO Monday, January 11, 2016 6:10 PM
    Monday, January 11, 2016 6:05 PM
  • If you really want the name then there is no "Fullname" property but you can do this.

    Get-DistributionGroup | %{
    		$_.managedby | %{(Get-AdUser $_.DistinguishedName -Prop DisplayName).DisplayName}
    	}
    


    \_(ツ)_/

    Monday, January 11, 2016 6:10 PM
  • OK, so you are saying that the distinguishedName of your objects are employee numbers.

    If you want the displayName property from each managedBy distinguishedName, you have to ask for it.


    $params = @{
      "LDAPFilter" = "(&(msExchRecipientDisplayType:1.2.840.113556.1.4.803:=1)(managedBy=*))"
      "Properties" = "managedBy"
    }
    Get-ADGroup @params | ForEach-Object {
      $managedBy = $_.managedBy
      foreach ( $managerDN in $managedBy ) {
        New-Object PSObject -Property @{
          "Group" = $_.DistinguishedName
          "managedBy_displayName" = (Get-ADObject $managerDN -Properties displayName).displayName
        } | Select-Object Group,managedBy_displayName
      }
    }
    


    -- Bill Stewart [Bill_Stewart]

    Monday, January 11, 2016 6:11 PM
    Moderator
  • jrv

    This worked but only displayed the Full name. How do I display the name of the DL also?

    • Edited by WREED_CISCO Monday, January 11, 2016 6:15 PM
    Monday, January 11, 2016 6:13 PM
  • OK, so you are saying that the distinguishedName of your objects are employee numbers.

    If you want the displayName property from each managedBy distinguishedName, you have to ask for it.


    $params = @{
      "LDAPFilter" = "(&(msExchRecipientDisplayType:1.2.840.113556.1.4.803:=1)(managedBy=*))"
      "Properties" = "managedBy"
    }
    Get-ADGroup @params | ForEach-Object {
      $managedBy = $_.managedBy
      foreach ( $managerDN in $managedBy ) {
        New-Object PSObject -Property @{
          "Group" = $_.DistinguishedName
          "managedBy_displayName" = (Get-ADObject $managerDN -Properties displayName).displayName
        } | Select-Object Group,managedBy_displayName
      }
    }
    


    -- Bill Stewart [Bill_Stewart]

    Worked like a champ! Now how do I clean up the name of the DL? Right now it has its distinguished name. Can we make the name of the DL pretty?
    Monday, January 11, 2016 6:14 PM
  • You need to be more specific (notice it took several messages for us to figure out that your distinguished names contain employee numbers). What do you mean by "the name of the DL"? The name of the DL isn't in its distinguishedName attribute (Group column)?

    -- Bill Stewart [Bill_Stewart]

    Monday, January 11, 2016 6:15 PM
    Moderator
  • This gets you the name.

    Get-DistributionGroup | %{
    		$_.managedby %{$_.DistinguishedName}
    	}


    \_(ツ)_/


    I get this:

     : Method invocation failed because [Microsoft.Exchange.Data.Directory.ADMultiValuedProperty`1[[Microsoft.Exchange.Dat
    .Directory.ADObjectId, Microsoft.Exchange.Data.Directory, Version=14.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad
    64e35]]] doesn't contain a method named 'op_Modulus'.
    t C:\reports\test.ps1:1 char:26
     Get-DistributionGroup | % <<<< {
       + CategoryInfo          : InvalidOperation: (op_Modulus:String) [ForEach-Object], RuntimeException
       + FullyQualifiedErrorId : MethodNotFound,Microsoft.PowerShell.Commands.ForEachObjectCommand


    It copied wrong.  Try it again,

    \_(ツ)_/

    Monday, January 11, 2016 6:15 PM
  • Sorry I am trying to do this fast.

    Here is what your command spit out:

    CN=Professional Practice - MICU,OU=Distribution 1,OU=Dis...               Bekri, Doe

    Which is perfect! I only want to see "Professional Practice - MICU"  Not all the other garbage. This is for management.

    Monday, January 11, 2016 6:16 PM
  • Copied and pasted twice and no go.
    Monday, January 11, 2016 6:19 PM
  • Copied and pasted twice and no go.

    This works for me.  You likely copied it wrong or have an old copy.

    Get-DistributionGroup | %{
         $_
    .managedby | %{(Get-AdUser$_.DistinguishedName -Prop DisplayName).DisplayName}
    }


    \_(ツ)_/

    Monday, January 11, 2016 6:21 PM
  • And here is what I pasted for the DistinguishedName and it too works correctly.

    Get-DistributionGroup | %{
         $_
    .managedby | %{$_.DistinguishedName}
    }


    \_(ツ)_/

    Monday, January 11, 2016 6:22 PM
  • OK this work but only displays one column, the username. I need the DL name in one column and the username in another column. 
    Monday, January 11, 2016 6:25 PM
  • CN=Professional Practice - MICU,OU=Distribution 1,OU=Dis...               Bekri, Doe

    Which is perfect! I only want to see "Professional Practice - MICU" Not all the other garbage. This is for management.

    The "other garbage" to which you refer is the group's distinguished name. If you want just the name property:


    $params = @{
      "LDAPFilter" = "(&(msExchRecipientDisplayType:1.2.840.113556.1.4.803:=1)(managedBy=*))"
      "Properties" = "managedBy"
    }
    Get-ADGroup @params | ForEach-Object {
      $managedBy = $_.managedBy
      foreach ( $managerDN in $managedBy ) {
        New-Object PSObject -Property @{
          "Group" = $_.Name
          "Manager" = (Get-ADObject $managerDN -Properties displayName).displayName
        } | Select-Object Group,Manager
      }
    }
    


    -- Bill Stewart [Bill_Stewart]

    Monday, January 11, 2016 6:25 PM
    Moderator
  • Sorry I am trying to do this fast.

    Here is what your command spit out:

    CN=Professional Practice - MICU,OU=Distribution 1,OU=Dis...               Bekri, Doe

    Which is perfect! I only want to see "Professional Practice - MICU"  Not all the other garbage. This is for management.

    Monday, January 11, 2016 6:25 PM
  • Try my reply directly above your most recent response.

    -- Bill Stewart [Bill_Stewart]

    Monday, January 11, 2016 6:26 PM
    Moderator
  • It worked thank you bill! You just taught me alot also.
    Monday, January 11, 2016 6:27 PM
  • Be more precise in your language and you will get faster/more accurate answers. "Other garbage" isn't a technical term <grin>


    -- Bill Stewart [Bill_Stewart]

    Monday, January 11, 2016 6:30 PM
    Moderator
  • Oh c'mon! Haha you never say just hit the damn computer with a hammer? lol jk

    Now on to the next issue. Your script is only outputting the first owner of the DL. We have lots of DLs that have 2, 3, maybe even 4+ owners. How do we extract ALL owners also?

    Monday, January 11, 2016 6:33 PM
  • Are all the group owners stored in the managedBy attribute for the group object?

    If not, then the script I posted will be ineffective.


    -- Bill Stewart [Bill_Stewart]

    Monday, January 11, 2016 6:37 PM
    Moderator
  • Oh c'mon! Haha you never say just hit the damn computer with a hammer? lol jk

    Now on to the next issue. Your script is only outputting the first owner of the DL. We have lots of DLs that have 2, 3, maybe even 4+ owners. How do we extract ALL owners also?

    Ok - I will let you guys beat your heads against a wall.  I posted the hint as to how t do that a long time ago:

    Get-DistributionGroup | %{
         $_
    .managedby | %{(Get-AdUser$_.DistinguishedName -Prop DisplayName).DisplayName}
    }

    This gets all managers.  Just add group name and you are done.  Noneed for fancy programming.


    \_(ツ)_/

    Monday, January 11, 2016 6:37 PM
  • Are all the group owners stored in the managedBy attribute for the group object?

    If not, then the script I posted will be ineffective.


    -- Bill Stewart [Bill_Stewart]

    Yes sir they are. But your script only outputs the first owner.
    Monday, January 11, 2016 6:40 PM
  • Oh c'mon! Haha you never say just hit the damn computer with a hammer? lol jk

    Now on to the next issue. Your script is only outputting the first owner of the DL. We have lots of DLs that have 2, 3, maybe even 4+ owners. How do we extract ALL owners also?

    Ok - I will let you guys beat your heads against a wall.  I posted the hint as to how t do that a long time ago:

    Get-DistributionGroup | %{
         $_
    .managedby | %{(Get-AdUser$_.DistinguishedName -Prop DisplayName).DisplayName}
    }

    This gets all managers.  Just add group name and you are done.  Noneed for fancy programming.


    \_(ツ)_/

    Where do I add the group name? I am not a powershell guy. If you tell me how to get that, I think we will be done here if yours spits out all owners.
    Monday, January 11, 2016 6:41 PM
  • Get-DistributionGroup | %{
        	$name=$_.Name
    	$_.managedby | %{
    		[pscustomobject]@{
    			GroupName=$name
    			MangerName=(Get-AdUser $_.DistinguishedName -Prop DisplayName).DisplayName
    		}
    	}
    }
    
    


    \_(ツ)_/

    Monday, January 11, 2016 6:45 PM
  • Get-DistributionGroup | %{
        	$name=$_.Name
    	$_.managedby | %{
    		[pscustomobject]@{
    			GroupName=$name
    			MangerName=(Get-AdUser $_.DistinguishedName -Prop DisplayName).DisplayName
    		}
    	}
    }
    


    \_(ツ)_/

    The output of this will not work.

    I need two columns:

    Distribution List Name             Owner

    Here is how it looks.

    Monday, January 11, 2016 6:49 PM
  • Fired up my Exchange test environment, and I can see that the managedBy attribute contains canonical names.

    Try this variation. You will need the Translate-ADName.ps1 script from this article:

    Windows IT Pro: Translating Active Directory Object Names Between Formats


    Get-DistributionGroup | Where-Object { $_.ManagedBy } | ForEach-Object {
      $ManagedBy = $_.ManagedBy
      foreach ( $managerCN in $managedBy ) {
        New-Object PSObject -Property @{
          "Group" = $_.Name
          "Manager" = Translate-ADName.ps1 display $managerCN
        } | Select-Object Group,Manager
      }
    }
    


    -- Bill Stewart [Bill_Stewart]

    Monday, January 11, 2016 6:53 PM
    Moderator
  • Actually it is a bit easier than I thought. The Translate-ADName.ps1 script isn't needed. Try it this way:


    Get-DistributionGroup | Where-Object { $_.ManagedBy } | ForEach-Object {
      $managedBy = $_.ManagedBy
      foreach ( $managerId in $managedBy ) {
        New-Object PSObject -Property @{
          "Group" = $_.Name
          "Manager" = $managerId.Name
        } | Select-Object Group,Manager
      }
    }
    

    (I'm not as familiar with the object types used by the Exchange cmdlets.)


    -- Bill Stewart [Bill_Stewart]



    Monday, January 11, 2016 7:05 PM
    Moderator
  • Actually it is a bit easier than I thought. The Translate-ADName.ps1 script isn't needed. Try it this way:


    Get-DistributionGroup | Where-Object { $_.ManagedBy } | ForEach-Object {
      $ManagedBy = $_.ManagedBy
      foreach ( $managerId in $managedBy ) {
        New-Object PSObject -Property @{
          "Group" = $_.Name
          "Manager" = $managerId.Name
        } | Select-Object Group,Manager
      }
    }
    

    (I'm not as familiar with the object types used by the Exchange cmdlets.)


    -- Bill Stewart [Bill_Stewart]


    I will, the last script with ADTranslate is running now. Appears to be working. We have like 15,000 DLs so it takes a while.
    • Edited by WREED_CISCO Monday, January 11, 2016 7:06 PM
    Monday, January 11, 2016 7:06 PM
  • Get-DistributionGroup | %{
        	$name=$_.Name
    	$_.managedby | %{
    		[pscustomobject]@{
    			GroupName=$name
    			MangerName=(Get-AdUser $_.DistinguishedName -Prop DisplayName).DisplayName
    		}
    	}
    }


    \_(ツ)_/

    The output of this will not work.

    I need two columns:

    Distribution List Name             Owner

    Here is how it looks.

    Yes - You are running PS2 which doesn't support pscustomobject in that way:

    Use this instead:

    Get-DistributionGroup | %{
             $name=$_.Name
    	 $_.managedby | %{
             New-Object PsObject -property @{
                 		GroupName=$name
                 		MangerName=(Get-AdUser $_.DistinguishedName -Prop DisplayName).DisplayName
             	}
         }
     }

    You should upgrade


    \_(ツ)_/



    • Edited by jrv Monday, January 11, 2016 7:13 PM
    Monday, January 11, 2016 7:11 PM
  • $DGroups=Get-DistributionGroup *
    $mastertable=@()
    ForEach($Group in $DGroups){
        $table=[pscustomobject][ordered]@{
                                    Name = $group.name
                                    "Managed By" = $group.managedby.name
                                    
    
                                        }
    $Mastertable += $table
    
    }
    
    
    $Mastertable |  export-csv "your export path.csv" 
    

    This should do what you want. You were trying to 'Get-ADuser' when naming the property. This property exists already as '.managedby.name'.
    Monday, January 11, 2016 7:20 PM
  • Yeah I am doing this on a 2008 R2 server because my 8.1 box I cannot get the Exchange Snap-In to load with the AD snapin.
    Monday, January 11, 2016 7:23 PM
  • This worked perfect, thank you both for all the help! I really appreciate it.
    Monday, January 11, 2016 7:25 PM
  • My most recent script post in this thread will run even faster since it doesn't translate the names before outputting.


    -- Bill Stewart [Bill_Stewart]

    Monday, January 11, 2016 7:31 PM
    Moderator