1

I am looking to use PowerShell's Select-String function within Visual Studio.

I have written a blog post about how to view Select-String output within Notepad++ taking advantage of NppExec's ability to call an external program, and its ability to add a custom line pattern matching to match <path>:<line-number>:<line-content>.

Is there any way of doing this within Visual Studio, perhaps by spitting out text to the Output Window?

Tahir Hassan
  • 5,715
  • 6
  • 45
  • 65

1 Answers1

3

VS indeed interprets text in the output window and if it finds something like

<filename>(<linenumber>):

doubleclicking that line navigates to the given line number in that file, and it's no big deal transforming the output of Select-String to this particluar format.

Looking at your blog post, I kinda like the idea (+1 for that) but I'm not too keen on the workflow: suppose I'm in VS I don't really want to switch to the console, then type text, then switch back to VS and execute some command. Then I'd rather just use the existing Find in Files functionality, eventually with regular expressions. Does pretty much the same, faster and handier.

However there's always room for handy automation and I definitely see some uses for your idea so here's an example of how to leverage the External Tools funcionality with PS's dir | sls to look for all matches of the currently selected text in all files in the directory of the current file:

  • in VS, select Tools->External Tools...
  • select Add, set suitable Title, set powershell.exe as command
  • set the Arguments to -NoProfile -Command dir -r $(ItemDir) | Select-String -simplematch '$(CurText)' | ForEach-Object { '{0}({1}): {2}' -f $_.Path, $_.LineNumber, $_.Line }
  • check Use Output window, click Ok

Now open a file in VS and select text, then select Tools->YourCommandTitle and doubleclick any matches (there will be at least one since the current file is included in the search).

Adjust the command to taste (look here for more built-in arguments), assign a keyboard shortcut to it and off you go!

stijn
  • 34,664
  • 13
  • 111
  • 163