Reputation: 13
Can somebody help me with powershell select string please.
Example I have a link "https://abv.com/personal/John/Documents/01/Events/02/NO_SHOW.xlsx"
Is there anyway I get use powershell and get only data after 5 "/" like this: "https://abv.com/personal/John/"
Thank you very much.
Upvotes: 1
Views: 73
Reputation: 26325
You could use Split()
to split the URL on '/'
, Select-Object
to select the first 5
parts, and Join-String
to join the parts back up with '/'
:
PS C:\> $url = 'https://abv.com/personal/John/Documents/01/Events/02/NO_SHOW.xlsx'
PS C:\> $url.Split('/') | Select-Object -First 5 | Join-String -Separator '/'
https://abv.com/personal/John
If you want the rest of the data, use -Last 5
to get the last five parts instead:
PS C:\> $url.Split('/') | Select-Object -Last 5 | Join-String -Separator '/'
Documents/01/Events/02/NO_SHOW.xlsx
Upvotes: 1
Reputation: 839
May be:
("https://abv.com/personal/John/Documents/01/Events/02/NO_SHOW.xlsx" -split '/')[0..4] -join '/'
or if you need the last '/'
then:
(("https://abv.com/personal/John/Documents/01/Events/02/NO_SHOW.xlsx" -split '/')[0..4] -join '/') + '/'
If you need the other part of the string you may take this approach:
#contains all the details
$addressArray = "https://abv.com/personal/John/Documents/01/Events/02/NO_SHOW.xlsx" -split '/'
#contains only the first five
$firstFive = ($addressArray[0..4] -join '/') + '/'
#contains the rest of the symbols
$theRest = $addressArray[5..($addressArray.Count -1)] -join '/'
Hope it helps!
Upvotes: 2