Select part of string

This topic contains 7 replies, has 4 voices, and was last updated by Profile photo of Mark Prior Mark Prior 10 months ago.

  • Author
    Posts
  • #54707
    Profile photo of Mark Prior
    Mark Prior
    Participant

    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

  • #54708
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant
    '{global.xxxx.com/Europe/United Kingdom/London Campus/Human Resources/Human Resources (Europe)/Users/Elaine Bates}' -match '.*/(.+)}'
    $Matches[1]
  • #54709
    Profile photo of Dan Potter
    Dan Potter
    Participant

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

  • #54710
    Profile photo of Dan Potter
    Dan Potter
    Participant

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

  • #54711
    Profile photo of Mark Prior
    Mark Prior
    Participant

    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

  • #54712
    Profile photo of Mark Prior
    Mark Prior
    Participant
    $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

    • This reply was modified 10 months ago by Profile photo of Mark Prior Mark Prior.
    • #54721
      Profile photo of Liam Fisher
      Liam Fisher
      Participant

      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

  • #54722
    Profile photo of Mark Prior
    Mark Prior
    Participant

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

You must be logged in to reply to this topic.