Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a PowerShell script from Visual Studio 2010

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

like image 295
Don Rolling Avatar asked May 17 '11 19:05

Don Rolling


People also ask

How do I run a PowerShell script in Visual Studio?

Once it is saved as a PS1, VS Code will identify the file as a PowerShell script. From there, you can execute the PowerShell script by press F5 . You can also click the Run button on the top right of the editor. To run a select, you can press F8 or right click on the selection and click the Run Selection option.

How do I open PowerShell in Visual Studio?

Start in Visual Studio Open Visual Studio. On the menu bar, select Tools > Command Line > Developer Command Prompt or Developer PowerShell.

Can you use PowerShell in Visual Studio?

The Microsoft PowerShell extension for Visual Studio Code (VS Code) provides rich language support and capabilities such as syntax completions, definition tracking, and linting for PowerShell. The extension works anywhere you can run VS Code and PowerShell 7 or higher.


2 Answers

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.)

like image 160
Mark Melville Avatar answered Oct 05 '22 17:10

Mark Melville


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.

like image 44
nosirrahcd Avatar answered Oct 05 '22 17:10

nosirrahcd