0

I have been trying to get to write a regex that ignores all text post the fifth occurrence of backslash, e.g below text; language in question is powershell \\Fileserver\Usershare$\Svr2008\Profiles\john.contoso.V2 I made multiple attempts but no avail, the best i could do was match everything post usershare$, using ([^\$]*$) which gives me \\Fileserver\Usershare$

I would like the output to be \\Fileserver\Usershare$\svr2008

Any help would be appreciated.

As suggested by Mik i tried ^\\\\([^\\]*\\){3} and used it as

$Parent= \\Fileserver\Usershare$\Svr2008\Profiles\john.contoso.V2
$test = $parent -replace '^\\\\([^\\]*\\){3}'
$test

I get the below

Svr2008\Profiles\john.contoso.V2

However i require the part that didn't match, am i doing something wrong?

  • The regex does not "output" anything. It is a pattern matching tool. You can use it to replace the matched substring with some pattern. This is what you want: to replace anything after the fifth backslash with empty string? – ZorgoZ Dec 02 '19 at 10:11
  • Yep, I'm using the replace function in powershell post match – Rohit Kachroo Dec 02 '19 at 10:14
  • 1
    There's a non-regex solution here: https://stackoverflow.com/questions/55029472/list-folders-at-or-below-a-given-depth-in-powershell. Depending on your script, it might be better to only obtain the items you need in the first place using e.g. Get-ChildItem -Path x -Depth 4 – Scepticalist Dec 02 '19 at 10:21
  • are you actually wanting just the path - with the file name removed? – Lee_Dailey Dec 02 '19 at 10:32
  • @Lee_Dailey: No i actually require the path with the file name as well as the parent directory of the file removed – Rohit Kachroo Dec 02 '19 at 11:04
  • @RohitKachroo - ah! i misunderstood your intent. have you looked at `Split-Path`? the `-Parent` will return the parent dir. do that twice to get up two levels. also, if your path is a dirinfo object, you can use `$Dir.Parent.Parent`. lastly, you can use standard string split/join commands - >>> `$Test.Split('\')[0..4] -join '\'` <<< if `$Test` contains your posted path, the the output will be `\\Fileserver\Usershare$\Svr2008`. – Lee_Dailey Dec 02 '19 at 11:14

2 Answers2

0

Try this one (it assumes there are 5+ backslashes)

^\\\\([^\\]*\\){3}

It matches \\Fileserver\Usershare$\Svr2008\ in \\Fileserver\Usershare$\Svr2008\Profiles\john.contoso.V2

After doing '\\Fileserver\Usershare$\Svr2008\Profiles\john.contoso.V2' -matches '^\\\\([^\\]*\\){3}', your desired string will be in $matches[0]

MiK
  • 918
  • 4
  • 16
  • Works like a charm! But when i replace it removes the part i need, so $test = $parent -replace '^\\\\([^\\]*\\){3}' gives output as Profiles\john.contoso.V2. – Rohit Kachroo Dec 02 '19 at 11:05
  • 1
    I edited in some more info, your desired string will be in $matches[0] – MiK Dec 02 '19 at 12:13
0

here's yet another way to get the job done. [grin]

what it does ...

  • puts the path in a $Var
  • splits on the \ chars
  • grabs the 1st five resulting items
  • joins them with \

the code ...

$Test = '\\Fileserver\Usershare$\Svr2008\Profiles\john.contoso.V2'
$Test.Split('\')[0..4] -join '\'

output = \\Fileserver\Usershare$\Svr2008

Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26