user11221802
user11221802

Reputation: 15

PowerShell - Select only a portion of returned property

Can anyone give me a quick tip on how to return only a portion of a property from the following cmdlet?

get-mobiledevice | select UserDisplayName,DeviceType,DeviceModel

The UserDisplayName property comes back as.. domain.local/OU Name/OU Name/First Last

I only want to return "Frist Last" and strip out the full domain and OU path.

Regards, Adam Tyler

Upvotes: 0

Views: 597

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60045

This is what I personally use when I need to split a canonicalName:

$canonical='domain.local/OU Name/OU Name/First Last'
Split-Path $canonical -Leaf

# This also works:
$canonical.Substring($canonical.LastIndexOf('/')+1)

In your example, the code should look like this:

# This:
$expression={
    Split-Path $_.UserDisplayName -Leaf
}

# Or This:
$expression={
    $_.UserDisplayName.Substring($_.UserDisplayName.LastIndexOf('/')+1)
}

Get-MobileDevice | select @{n='UserDisplayName';e=$expression},DeviceType,DeviceModel

Upvotes: 1

Related Questions