Home > Net >  PowerShell pipe ExpandProperty to another Select-Object
PowerShell pipe ExpandProperty to another Select-Object

Time:10-19

I want to get the aliases for all email recipients in my O365 tenant. If I were doing this one traditional Exchange, the easiest is to Get-Recipient and pull out the SMTPAddress property using Exchange Manangement Console. (Note the Select piped into another Select; works like a champ.)

Get-Recipient | Select Name,SamAccountName -ExpandProperty EmailAddresses | Select Name,SamAccountName,SmtpAddress

The issue is when I try to do the same thing in EXO, the "EmailAddresses" property is output as a flat string and so when I try to pipe it to a 2nd select, the only Property is the Length, not the actual value of the string.

The following kind of works, but I feel like there has to be a better way?

Get-EXORecipient | Select Name -ExpandProperty EmailAddresses | % {Write-Output "$($_.Name),$($_)"}

CodePudding user response:

What -ExpandProperty EmailAddresses expands to are strings, as you state.

The additional properties that these strings are decorated with if you also pass (positionally implied) -Property arguments, such as Name in your example, are present, but will typically not surface unless you explicitly access them on the now-decorated output strings.

If you want output objects that contain each email address as a property, more work is needed.

Get-EXORecipient | ForEach-Object {
  foreach ($emailAddress in $_.EmailAddresses) {
    [pscustomobject] @{
      EmailAddress = $emailAddress
      Name = $_.Name 
      # ... add further properties of interest here
    }
  }
}
  • Related