Remove path from output Remove path from output powershell powershell

Remove path from output


Select-String outputs an object from which you can pick off properties that you want. The Get-Member command will show you these object members if you pipe into it e.g.:

Select-String -Path E:\Documents\combined0.txt -Pattern "GET /ccsetup\.exe" -AllMatches  |     Get-Member

One of those properties is Line. So try it this way:

Select-String -Path E:\Documents\combined0.txt -Pattern "GET /ccsetup\.exe" -AllMatches |     Foreach {$_.Line} > E:\Documents\combined3.txt


As usual powershell returns things as objects, by default select-string returns several properties including LineNumber, Filename, etc; the one you want with the data in is just called "Line". So no need for anything fancy, just pipe it to "select line".

Eg:

Select-String "bla" filename.txt | select-object -ExpandProperty Line | out-file E:\bla_text.txt


If you're looking for (sub)strings rather than patterns, using the -like operator might be a better approach, performance-wise and with respect to ease-of-use.

$searchString = 'GET /ccsetup.exe'Get-Content 'E:\Documents\combined0.txt' |  ? { $_ -like "*$searchString*" } |  Set-Content 'E:\Documents\combined3.txt'

If you do need pattern matches, you can easily replace the -like operator with the -match operator:

$pattern = 'GET /ccsetup\.exe'Get-Content 'E:\Documents\combined0.txt' |  ? { $_ -match $pattern } |  Set-Content 'E:\Documents\combined3.txt'