17

If I have a .ps file in Visual Studio, how do I go about running it within the context of the project?

4 Answers 4

16

I did this by adding a context menu item linked to an external tool:

Add an "External Tool". Go to Tools > External Tools. Add a new one with these settings:

  • Title: Run with Powershell
  • Command: powershell.ex
  • Arguments: -ExecutionPolicy RemoteSigned -File "$(ItemPath)"
  • Initial Directory: $(ItemDir)
  • Check "Use Output Window"
  • If you plan to use scripts that require arguments, check "Prompt For Arguments"

Take note of the position your tool is in the list (1,2, etc...) Click OK.

  • Now go to Tools > Customize, Commands tab, select Context Menu or Toolbar, and choose "Project and Solution Context Menus | Item".
  • Click "Add Command...".
  • Choose Tools category, and choose "External Command X" where x is the position your tool was in the list (index starts with 1, not 0).
  • Click OK.
  • Move it to the position you want in the menu, and click "Modify Selection" to give it a friendly name,
  • add keyboard shortcuts, etc..
  • Click Close.

Right click your .ps1 file in the solution explorere and enjoy. (NOTE: I also did this for cmd.exe to run .bat files.)

Sign up to request clarification or add additional context in comments.

Comments

4

Not sure what exactly you mean by "from within the context of the project" but you can:

  1. Create a Process

    Process p = new Process();
    
  2. Then set the command of the process to be:

    powershell.exe YourScriptName.ps1
    

This works if you just need to run the script.

1 Comment

That's great! Thanks. I need to alter a project file's xml, but am having a hard time accessing the path to the project dynamically, because this script will not run in the same location all of the time.
4

The best way to do this is to install the PowerGUI extension for Visual Studio 2010

One of the features of this extension is an integrated powershell tool window. This has access to automation objects like DTE via $EnvDTE and will provide a good environment for running the script against the project

3 Comments

How does that compare to using NuGet? I would assume that the NuGet console is hooked into the DTE somehow.
Well NuGet uses powershell when it installs....I'm actually researching powershell for that reason.
Jared, that's useful. I already have PowerGUI and I'm a little confused about how to use it. My biggest issue is that the scripts I've tried to run say that they're compiled with a newer version than what I have, but I just dl'd it. Still working on this one.
4

You should also look at StudioShell, which provides a deep integration of the DTE in a custom PowerShell host.

1 Comment

Wow, StudioShell is pretty awesome!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.