Home > Software engineering >  Powershell Select-String not finding what I expect
Powershell Select-String not finding what I expect

Time:11-27

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?

CodePudding user response:

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

CodePudding user response:

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
  • Related