Reputation: 2339
If I have a .ps file in Visual Studio, how do I go about running it within the context of the project?
Upvotes: 17
Views: 16321
Reputation: 813
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:
Run with Powershell
powershell.ex
-ExecutionPolicy RemoteSigned -File "$(ItemPath)"
$(ItemDir)
Use Output Window
"Prompt For Arguments
"Take note of the position your tool is in the list (1,2, etc...) Click OK.
Tools
> Customize
, Commands
tab, select Context Menu
or Toolbar
, and choose "Project and Solution Context Menus | Item".Add Command...
".Tools
category, and choose "External Command X
" where x
is the position your tool was in the list (index starts with 1, not 0).OK
.Right click your .ps1 file in the solution explorere and enjoy. (NOTE: I also did this for cmd.exe to run .bat files.)
Upvotes: 16
Reputation: 1151
You should also look at StudioShell, which provides a deep integration of the DTE in a custom PowerShell host.
Upvotes: 4
Reputation: 1467
Not sure what exactly you mean by "from within the context of the project" but you can:
Create a Process
Process p = new Process();
Then set the command of the process to be:
powershell.exe YourScriptName.ps1
This works if you just need to run the script.
Upvotes: 4
Reputation: 754853
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
Upvotes: 4