Extract Certain Length of Characters From Delimiter

Welcome Forums General PowerShell Q&A Extract Certain Length of Characters From Delimiter

Viewing 4 reply threads
  • Author
    Posts
    • #194639
      Participant
      Topics: 21
      Replies: 41
      Points: 303
      Rank: Contributor

      Hello,

      I’m trying to extract certain content from the return of one of my scripts. The return value has a delimiter of ‘/’.

      Here’s an example:

      /subscriptions/f873d82f-7491-4045-8dbe/resourceGroups/use2-dev-rg

      I’d like to pull the value after the last delimiter ‘/’.

      Any idea on how to extract that content?

      Thanks,
      Frank

    • #194660
      Participant
      Topics: 24
      Replies: 175
      Points: 596
      Helping Hand
      Rank: Major Contributor
      $value = “/subscriptions/f873d82f-7491-4045-8dbe/resourceGroups/use2-dev-rg” -split “/”
      
      $lastsplit = $value[$value.count -1]
      
      

      heres one approach, if the number of / will always be the same, then you can reference it as $value[4]

       

       

    • #194663
      Participant
      Topics: 13
      Replies: 1683
      Points: 2,849
      Helping Hand
      Rank: Community Hero

      Another method is using GetUpperBound:

      $value = "/subscriptions/f873d82f-7491-4045-8dbe/resourceGroups/use2-dev-rg" -split "/"
      $value[$value.GetUpperBound(0)]
      
    • #194681
      Participant
      Topics: 5
      Replies: 323
      Points: 487
      Helping Hand
      Rank: Contributor
      # Get last string in array after splitting
      $string = '/subscriptions/f873d82f-7491-4045-8dbe/resourceGroups/use2-dev-rg'
      $string.Split('/')[-1]
      
    • #194699
      Participant
      Topics: 21
      Replies: 41
      Points: 303
      Rank: Contributor

      All of them work great, but the I’m going to use:

      @{Expression={$_.Scope.Split(‘/’)[-1]};Label=”Scope”;width=100}
      

      It was easier to add to my existing code than the others and returns exactly what I need.

      I appreciate everyone’s help! Thx!

Viewing 4 reply threads
  • The topic ‘Extract Certain Length of Characters From Delimiter’ is closed to new replies.