PowerShell – Get Notes of an Exchange Distribution Group

active-directoryexchangepowershellwindows

I want to build a One-Liner to Get the Notes of a specific Distribution Group and output it alongside the Name and other information of the Distribution Group.

I've googled it and found differed sources with all the same solutions.
This is one of the Solutions I found:

https://richgski.blogspot.com/2012/03/powershell-get-exchange-distribution.html

Get-DistributionGroup Head-of-Operations | Select-Object Name, GroupType, ManagedBy, @{Name="Notes";Expression={(Get-Group $_).Notes}}

However, the Row with the Notes will always stay blank, and I don't know why:

Name        GroupType ManagedBy Notes
----        --------- --------- -----
Head-of-Ops Universal {}

When I do the following command separately:

Get-Group Head-of-Ops | Select-Object Notes

… it's giving me the correct Note as Output:

Notes
-----
Owner- Paul J.

I little later I checked if I have the correct parameters, so I tried to do it this way:

$Result = Get-DistributionGroup Head-of-Operations
Get-Group $Result.Name | Select-Object Notes

It worked. Output:

Notes
-----
Owner- Paul J.

I made some changes to my command, but when I try it this way, it's still not working:

Get-DistributionGroup Head-of-Operations | Select-Object Name,GroupType,ManagedBy,@{Name="Notes";Expression={(Get-Group $_.Name | Select-Object Notes)}}

Output:

Name        GroupType ManagedBy Notes
----        --------- --------- -----
Head-of-Ops Universal {}

After that, I found another Post to this Topic here: https://www.oxfordsbsguy.com/2014/04/21/exchange-powershell-how-to-enumerate-distribution-lists-managers-and-members/#comment-4452

So I made some changes to the command one more time…

Get-DistributionGroup Head-of-Ops | Select-Object Name,GroupType,ManagedBy,@{Expression={(Get-Group $_.Name).Notes};Label="Notes"}

… still, nothing has changed at all. Output:

Name        GroupType ManagedBy Notes
----        --------- --------- -----
Head-of-Ops Universal {}

I just don't get it :/

Does someone of you guys see the issue and can point me to it?

Kind regards,

Kevin van Thiel

p.S.: I've run this command with the -verbose parameter now and I think I'm now one step closer to the solution.
I think at some point it's just missing a parameter, but I don't know which one.This is the Code: (I've marked sensitive information with an * asterisk)

Get-DistributionGroup Head-of-Ops@h***.com -Verbose | Select-Object Name,GroupType,ManagedBy,@{Name="Notes";Expression={(Get-Group $_.Name).Notes}} -Verbose
VERBOSE: [16:04:28.885 GMT] Get-DistributionGroup : Active Directory session settings for 'Get-DistributionGroup' are: View Entire Forest: 'False', Default Scope: 'h***.de', Configuration Domain Controller: 'H***.h***.de',
Preferred Global Catalog: '***.h***.h***.de', Preferred Domain Controllers: '{ ****.h***.h***.de, H***.h***.de }'
VERBOSE: [16:04:28.916 GMT] Get-DistributionGroup : Runspace context: Executing user: h***.de/Companies/H***/D***/User/IT Service/****, Executing user organization: , Current organization: , RBAC-enabled: Enabled.
VERBOSE: [16:04:28.916 GMT] Get-DistributionGroup : Beginning processing &
VERBOSE: [16:04:28.932 GMT] Get-DistributionGroup : Current ScopeSet is: { Recipient Read Scope: {{, }}, Recipient Write Scopes: {{, }}, Configuration Read Scope: {{, }}, Configuration Write Scope(s): {{, }, }, Exclusive Recipient
Scope(s): {}, Exclusive Configuration Scope(s): {} }
VERBOSE: [16:04:28.932 GMT] Get-DistributionGroup : Resolved current organization: .
VERBOSE: [16:04:28.932 GMT] Get-DistributionGroup : Searching objects "Head-of-Ops@h***.com" of type "ADGroup" under the root "$null".
VERBOSE: [16:04:28.932 GMT] Get-DistributionGroup : Previous operation run on domain controller 'H***.h***.de'.
VERBOSE: [16:04:28.932 GMT] Get-DistributionGroup : Previous operation run on domain controller 'H***.h***.de'.
VERBOSE: [16:04:28.932 GMT] Get-DistributionGroup : Preparing to output objects. The maximum size of the result set is "1000".

VERBOSE: [16:04:28.947 GMT] Get-DistributionGroup : Ending processing &
Name        GroupType ManagedBy Notes
----        --------- --------- -----
Head-of-Ops Universal {}

The following Line is the one where I think the issue is buried:

VERBOSE: [16:04:28.932 GMT] Get-DistributionGroup : Searching objects "Head-of-Ops@h***.com" of type "ADGroup" under the root "$null".

Anyone any idea what it's missing and why?

Best Answer

When creating a calculated property with Select-Object you need

  1. an Expression, may abbreviated to just E/e
  2. a Name OR a Label, they are synonymic and may also be reduced to the 1st letter.

From your question it's unclear if Get-DistributionGroup Head-of-Operations returns a single or mutliple objects?

Also is Head-of-Operations the same as Head-of-Ops ?

An alternative way to adding properties via Select-Object is to create a [PSCustomObject]

foreach($HeadOP in Get-DistributionGroup Head-of-Operations){
    [PSCustomObject]@{
        Name      = $HeadOp.Name
        GroupType = $HeadOp.GroupType
        ManagedBy = $HeadOp.ManagedBy
        Notes     = (Get-Group $HeadOp.Name | Select-Object Notes)
    }
}

Eventually you need to -ExpandProperty Notes

        Notes     = (Get-Group $HeadOp.Name | Select-Object -ExpandProperty Notes)

or shorter

        Notes     = (Get-Group $HeadOp.Name).Notes