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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,560 questions
{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 111.4K Reputation points MVP
    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. Dale Kudusi 3,236 Reputation points
    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.