0

I am trying to parse file paths to just get the file name and I have a regex .*\

I'll use the following

Select-String -Pattern '.*\\' -InputObject $test -NotMatch

on a file path like C:\Users\User\Desktop\test.exe and it returns blank. If I remove the -NotMatch flag it returns the entire path. I tried using a regex tester so I know the regex is correct. What am I doing wrong?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Eric Goto
  • 63
  • 8

2 Answers2

2

Instead of using Select-String, use Split-Path -leaf.

mklement0
  • 382,024
  • 64
  • 607
  • 775
Eric Goto
  • 63
  • 8
0

Looks like -notmatch just ignores the whole line if there's a match. How about this? This is any number of characters that are not backslashes at the end of a line.

'C:\Users\User\Desktop\test.exe' | select-string [^\\]*$ | % matches | % value

test.exe
js2010
  • 23,033
  • 6
  • 64
  • 66
  • Oh, you are right. I thought it would return everything but the matching. What do the | and %s mean in your response? – Eric Goto Nov 26 '21 at 20:45
  • It's the same thing as `| foreach-object propertyname`, it has the same effect as `| select -expand propertyname`. – js2010 Nov 26 '21 at 20:50