Eric Goto
Eric Goto

Reputation: 63

Powershell Select-String not finding what I expect

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?

Upvotes: 0

Views: 601

Answers (2)

js2010
js2010

Reputation: 27606

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

Upvotes: 0

Eric Goto
Eric Goto

Reputation: 63

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

Upvotes: 2

Related Questions