Select part of string

Can never bend my head around these scenarios

Get-Mailbox Hello.UK.Pensions | select grantsendonbehalfto -ExpandProperty grantsendonbehalfto

Gives me the below

{global.xxxx.com/Europe/United Kingdom/London Campus/Human Resources/Human Resources (Europe)/Users/Elaine Bates}

How do i only select all charectors after the last “/” except the “}”, so in this case to end up withe “Elaine Bates”

thanks

'{global.xxxx.com/Europe/United Kingdom/London Campus/Human Resources/Human Resources (Europe)/Users/Elaine Bates}' -match '.*/(.+)}'
$Matches[1]

(Get-Mailbox Hello.UK.Pensions | select -ExpandProperty grantsendonbehalfto) -split ‘/’ | select -Last 1

you’ll have to foreach and join for any that include multiples.

thanks Olaf

thats great, however if i get multipe results ?

{global.xxxx.com/Europe/United Kingdom/London Campus/Human Resources/Human Resources (Europe)/Users/Elaine Bates}
{global.xxxx.com/Europe/United Kingdom/Newcastle/Users/Mark Prior}

need something with intelligence to pick out more than one name and ideally put it into a string or text file

$results = (Get-Mailbox hello.UK.Pensions | select -ExpandProperty grantsendonbehalfto) 

$names = foreach ($result in $results)
{
$result -split '/' | select -Last 1 
}

Thanks both have the above working, best thing about PS is theres always more than one way to achieve what you want

If you want to join your results up use the [string]::join(string separator, string[] values) method.

$Results = Get-Mailbox Hello.UK.Pensions | select grantsendonbehalfto -ExpandProperty grantsendonbehalfto
$ArrResults = $Results | ForEach-Object {$_ -split '/' | Select -Last 1}

# If you want to join them then use [string]::join(string seperator, string[] values)
$FriendlyResult = [string]::Join(', ', $ArrResults)

Liam

thanks liam, not needed but im sure i will use later down the line :slight_smile: