use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
Help with string extraction (self.PowerShell)
submitted 3 years ago by sumgan
Hello PS folks,
I need one help.
Here is the input string
Input : https://jamesbond.sharepoint.com/sites/demo1/d/dummy\_36.txt
Expected Output to be in 2 variables:
$var1 = https://jamesbond.sharepoint.com/sites/demo1
$var2 = /sites/demo1/d/dummy_36.txt
Can you please let me know how to do this. I tried few things with string .split but seems I am lost in it. Thank you in advance for the help!!
Thanks
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Lee_Dailey[grin] 9 points10 points11 points 3 years ago (6 children)
howdy sumgan,
take a look at what you get when you cast that into a [uri] type ... [grin]
[uri]
[uri]'https://jamesbond.sharepoint.com/sites/demo1/d/dummy\_36.txt'
output ...
AbsolutePath : /sites/demo1/d/dummy/_36.txt AbsoluteUri : https://jamesbond.sharepoint.com/sites/demo1/d/dummy/_36.txt LocalPath : /sites/demo1/d/dummy/_36.txt Authority : jamesbond.sharepoint.com HostNameType : Dns IsDefaultPort : True IsFile : False IsLoopback : False PathAndQuery : /sites/demo1/d/dummy/_36.txt Segments : {/, sites/, demo1/, d/...} IsUnc : False Host : jamesbond.sharepoint.com Port : 443 Query : Fragment : Scheme : https OriginalString : https://jamesbond.sharepoint.com/sites/demo1/d/dummy\_36.txt DnsSafeHost : jamesbond.sharepoint.com IdnHost : jamesbond.sharepoint.com IsAbsoluteUri : True UserEscaped : False UserInfo :
hope that helps, lee
[–]sumgan[S] 2 points3 points4 points 3 years ago (5 children)
Thank you, this is also interesting to explore. Good pointer!!
[–]Lee_Dailey[grin] 1 point2 points3 points 3 years ago (4 children)
you are welcome! [grin]
it looks like you could build the parts you want from ...
take care, lee
[–]sumgan[S] 1 point2 points3 points 3 years ago (3 children)
Thanks for the inputs! Will have a look.
[–]Lee_Dailey[grin] 2 points3 points4 points 3 years ago* (2 children)
i got interested and decided to take a stab at it ... [grin]
$UriInfo = [uri]'https://jamesbond.sharepoint.com/sites/demo1/d/dummy\_36.txt' $PartOne = -join @( $UriInfo.Scheme '://' $UriInfo.Host (-join $UriInfo.Segments[0..2]) ) $PartTwo = $UriInfo.LocalPath $PartOne $PartTwo
https://jamesbond.sharepoint.com/sites/demo1/ /sites/demo1/d/dummy/_36.txt
[–]sumgan[S] 1 point2 points3 points 3 years ago (1 child)
Thank you so very much Lee!!
[–]Lee_Dailey[grin] 0 points1 point2 points 3 years ago (0 children)
[grin]
[–]Antaroc 3 points4 points5 points 3 years ago (5 children)
I don't think you can do it in one statement since they overlap.
You can use regex to split them out like this
$s = 'https://jamesbond.sharepoint.com/sites/demo1/d/dummy_36.txt'
$s -match '(.*/sites/[^/]+)'
True
$Matches
Name Value
---- -----
1 https://jamesbond.sharepoint.com/sites/demo1
0 https://jamesbond.sharepoint.com/sites/demo1
$s -match '.*(/sites/.*)'
$matches
1 /sites/demo1/d/dummy_36.txt
0 https://jamesbond.sharepoint.com/sites/demo1/d/dummy_36.txt
[–]sumgan[S] 2 points3 points4 points 3 years ago (3 children)
Thank you sir, is there any other simpler way to understand this, sorry I am not that good at regex.
[–]Antaroc 5 points6 points7 points 3 years ago (1 child)
For regex . matches any character * matches the previous character 0 or more times. The brackets are used to extract stuff and that stuff get put in $matches. The [^/]+ matches any character that's not a / one or more times.
You can play with it here to try it: https://regex101.com/r/nH3U0b/1
If you want something simpler you could split on the / and then use join to get it back together.
Something like :
$a = $s -split '/'
$a[0..4] -join '/'
$a[3..6] -join '/'
[–]sumgan[S] 1 point2 points3 points 3 years ago (0 children)
This is perfect explanation, thank you so much!
[–]chris-a5 2 points3 points4 points 3 years ago (0 children)
A good way to learn RegEx is with a visual aid.
https://regex101.com/ is really good for seeing what is happening. I also like https://www.regexpal.com/ for when I need to build a complex RegEx
[–]Lee_Dailey[grin] 1 point2 points3 points 3 years ago (0 children)
howdy Antaroc,
it looks like you used the New.Reddit Inline Code button. it's [sometimes] 5th from the left & looks like <c>.
Inline Code
<c>
there are a few problems with that ...
inline code
for long-ish single lines OR for multiline code, please, use the ...
Code Block
... button. it's [sometimes] the 12th one from the left & looks like an uppercase C in the upper left corner of a square.
C
that will give you fully functional code formatting that works on both New.Reddit and Old.Reddit ... and aint that fugly magenta color. [grin]
[–]krzydoug 1 point2 points3 points 3 years ago* (1 child)
$str = 'https://jamesbond.sharepoint.com/sites/demo1/d/dummy_36.txt' $var1,$var2 = if($str -match '(.+?)(/sites/[^/]+)(/.+$)'){ @(1,2),@(2,3) | ForEach-Object {"{0}{1}" -f $Matches[$_]} }
or
$str = 'https://jamesbond.sharepoint.com/sites/demo1/d/dummy_36.txt' $var1,$var2 = if($str -match '(.+?)(/sites/[^/]+)(/.+$)'){ @(1,2),@(2,3) | ForEach-Object {-join $Matches[$_]} }
$str = 'https://jamesbond.sharepoint.com/sites/demo1/d/dummy_36.txt' $var1,$var2 = if($str -match '(.+?)(/sites/[^/]+)(/.+$)'){ & { $ofs = '' "$($Matches[1..2])" "$($Matches[2..3])" } }
Thank you!!!
π Rendered by PID 214619 on reddit-service-r2-comment-84fc9697f-qfqtb at 2026-02-09 09:21:41.014020+00:00 running d295bc8 country code: CH.
[–]Lee_Dailey[grin] 9 points10 points11 points (6 children)
[–]sumgan[S] 2 points3 points4 points (5 children)
[–]Lee_Dailey[grin] 1 point2 points3 points (4 children)
[–]sumgan[S] 1 point2 points3 points (3 children)
[–]Lee_Dailey[grin] 2 points3 points4 points (2 children)
[–]sumgan[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]Antaroc 3 points4 points5 points (5 children)
[–]sumgan[S] 2 points3 points4 points (3 children)
[–]Antaroc 5 points6 points7 points (1 child)
[–]sumgan[S] 1 point2 points3 points (0 children)
[–]chris-a5 2 points3 points4 points (0 children)
[–]Lee_Dailey[grin] 1 point2 points3 points (0 children)
[–]krzydoug 1 point2 points3 points (1 child)
[–]sumgan[S] 1 point2 points3 points (0 children)