First, to be clear: Format-*
cmdlets output objects whose sole purpose is to provide formatting instructions to PowerShell's output-formatting system - see this answer. In short: only ever use Format-*
cmdlets to format data for display, never for subsequent programmatic processing.
Here, the formatted output tells you that the information of interest is contained in the .Licenses
property, so, as Theo points out, you can simply access the property value by enclosing the command in ()
, the grouping operator:
# Outputs the .Licenses property value of the object output by Get-MsolUser.
(Get-MsolUser -UserPrincipalName $UPN).Licenses
As the property name (and the formatted output) implies, .Licenses
contains a collection of objects (possibly strings), and to extract only the part of each (stringified) object after the testmailorg:
prefix, you need to use text parsing, as Lee Dailey advises; here, the simplest approach is to use PowerShell's regex-based -replace
operator, which can directly operate on collections and performs string replacement on each element:
PS> (Get-MsolUser -UserPrincipalName $UPN).Licenses -replace '^.+:'
FLOW_FREE
M365EDU_A3_FACULTY
Note: If the LHS is a collection (array), the result of the -replace
operation is a corresponding array ([object[]]
) of the transformed strings.