Powershell -expandproperty syntax issue

Anonymous
2017-04-13T09:30:44+00:00

The following Powershell command does not give the results expected:

get-recipient | Select-Object -property DisplayName -ExpandProperty emailaddresses

As per (example 2):

https://msdn.microsoft.com/powershell/reference/5.1/Microsoft.PowerShell.Utility/Select-Object

What am I doing wrong?

Thanks in advance

David (Nobby) Barnes

Microsoft 365 and Office | Subscription, account, billing | For business | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Anonymous
    2017-04-24T20:44:10+00:00

    Hi David,

    After consulting our related team, we can achieve your goal via the following “piped-command”:

    Get-Recipient |ft Displayname, RecipientType, PrimarySmtpAddress, EmailAddresses –Wrap

    Thanks,

    Gary

    1 person found this answer helpful.
    0 comments No comments

12 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-04-19T12:34:43+00:00

    Hi Gary,

    thanks for the response on this..

    I tried this and cannot seem to find a way to expand the emailaddresses object list

    I tried the CSV output and all the email addresses are in one single cell for each user

    get-recipient | Select-Object -ExpandProperty emailaddresses

    Gets the closest but lacks the associated 'Display Name'

    As I said get-recipient is not allowing the correct results when -expandproperty is used

    even for a single user

    EG:

    get-recipient <user> | Select-Object -property DisplayName -ExpandProperty emailaddresses | FL

    If I use Get-process the -expandproperty usage does not break the command

    EG:

    Get-Process Explorer | Select-Object -Property ProcessName -ExpandProperty Modules | Format-List

    ProcessName       : explorer

    ModuleName        : Explorer.EXE

    FileName          : C:\WINDOWS\Explorer.EXE

    BaseAddress       : 140696870060032

    ModuleMemorySize  : 4661248

    EntryPointAddress : 140696870710112

    FileVersionInfo   : File:             C:\WINDOWS\Explorer.EXE

                        InternalName:     explorer

                        OriginalFilename: EXPLORER.EXE.MUI

                        FileVersion:      10.0.14393.0 (rs1_release.160715-1616)

                        FileDescription:  Windows Explorer

                        Product:          Microsoft® Windows® Operating System

                        ProductVersion:   10.0.14393.0

                        Debug:            False

                        Patched:          False

                        PreRelease:       False

                        PrivateBuild:     False

                        SpecialBuild:     False

                        Language:         English (United Kingdom)

    Site              :

    Container         :

    Size              : 4552

    Company           : Microsoft Corporation

    FileVersion       : 10.0.14393.0 (rs1_release.160715-1616)

    ProductVersion    : 10.0.14393.0

    Description       : Windows Explorer

    Product           : Microsoft® Windows® Operating System

    ProcessName       : explorer

    ModuleName        : ntdll.dll

    FileName          : C:\WINDOWS\SYSTEM32\ntdll.dll

    BaseAddress       : 140722574000128

    ModuleMemorySize  : 1904640

    EntryPointAddress : 0

    FileVersionInfo   : File:             C:\WINDOWS\SYSTEM32\ntdll.dll

                        InternalName:     ntdll.dll

                        OriginalFilename: ntdll.dll.mui

                        FileVersion:      10.0.14393.206 (rs1_release.160915-0644)

                        FileDescription:  NT Layer DLL

    Why doesn't get-recipient expand like this?

    I note that ProcessName is repeated and listed ahead of each module as the module list is expanded.

    Or am I missing something?

    Or have I found a bug?

    Thanks for looking at this

    David (Nobby) Barnes

    0 comments No comments
  2. Anonymous
    2017-04-20T09:19:45+00:00

    Hi David,

    Please run the following cmdlets to achieve your goal:

    $FormatEnumerationLimit =-1

    Get-Recipient |fl Displayname, EmailAddresses

    Thanks,

    Gary

    0 comments No comments
  3. Anonymous
    2017-04-20T11:41:14+00:00

    Hi Gary,

    Yes, I'd got that far but wanted to expand the emailaddresses object and cannot seem to.

    There must be a simpler way using pipe commands, but I cannot get the -expandproperty to work correctly.

    This codelet does work but doesn't output to CSV well.

    $Array = @()

    foreach ($user in get-recipient)

    {

    #Write-output $User

    $USR = (get-recipient $user.name | select DisplayName, Recipienttype, primarysmtpaddress)

    $PSMTP = $USR.primarysmtpaddress

    #$EMA = get-recipient $user | select -expandproperty emailaddresses | where { $_ -like “smtp:*” -and $_ -ne “smtp:$PSMTP” -and $_ -notlike “*onmicrosoft*” }

    $EMA = get-recipient $user.name | select -expandproperty emailaddresses | where { $_ -like “smtp:*” -and $_ -ne “smtp:$PSMTP” }

    $obj = New-Object PSObject

    $obj | Add-Member -MemberType NoteProperty -Name “Display Name” -Value $USR.DisplayName

    $obj | Add-Member -MemberType NoteProperty -Name “RecipientType” -Value $USR.recipienttype

    $obj | Add-Member -MemberType NoteProperty -Name “PrimarySMTPAddress” -Value $PSMTP

    foreach ($Emaddr in $EMA)

    {

    $EML = $Emaddr -split “:”,2

    $obj | Add-Member -MemberType NoteProperty -Name “EmailAddress” -Value $EML[1]

    $array += $obj

    $obj = New-Object PSObject

    }

    }

    Write-Output $array | FT

    There must be a direct 'piped-command' way of achieving this.

    Thanks

    David (Nobby) Barnes

    0 comments No comments
  4. Anonymous
    2017-04-21T11:22:44+00:00

    Hello David,

    I think we don’t need to expand the emailaddresses to get the result. 

    Try mine:

    Get-Recipient -ResultSize unlimited |Select-Object displayname,emailaddresses |Format-Table -wrap

    The format maybe not that ideal, but we can get the same result.

    Best,

    Alison

    0 comments No comments