How do I extract the last part of a url using PowerShell?

Mohammed 81 Reputation points
2020-12-31T14:21:10.727+00:00

I want get the last part of a URL, how do I get this done? How do I extract just 002-122 from this url https://mycompany.sharepoint.com/sites/Asite/002-122 with PowerShell?

Windows for business | Windows Server | User experience | PowerShell
{count} votes

3 answers

Sort by: Most helpful
  1. Titan 206 Reputation points
    2020-12-31T15:08:48.45+00:00

    Hello!

    You could use Split-Path "https://mycompany.sharepoint.com/sites/Asite/002-122" -Leaf


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    2 people found this answer helpful.

  2. Andreas Baumgarten 123.5K Reputation points MVP Volunteer Moderator
    2020-12-31T14:46:55.02+00:00

    Maybe this works for you:

    $url = 'https://mycompany.sharepoint.com/sites/Asite/002-122'
    $url.Split('/')[5]
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  3. Anonymous
    2021-01-01T08:31:43.58+00:00

    Hi,
    Check out these discussions on this thread about splitting parts of a URL:
    https://social.technet.microsoft.com/Forums/ie/en-US/38d06bcb-8842-47af-9d86-166dd4ee97e3/how-do-i-split-a-url?forum=winserverpowershell

    Best regards.

    **
    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.