How to execute a PowerShell script from Notepad++

cmd, notepad++, powershell

Solution

It took me a little fiddling, but I finally got this working. (I am using version 1.0 but this should work in other versions as well.)

Notepad++ can be set up to run commands, and assign shortcuts to those commands, as follows:

From the menu, click Run → Run

Add the command

C:\NotepadRun.bat "$(FULL_CURRENT_PATH)"

Save the command, giving it a name and a key shortcut.

Below are the contents of the batch file. I named mine `NotepadRun.bat`, but you can name it whatever.

@echo off

GOTO %~sx1
:.ps1
 cd "%~d1%~p1"
 powershell.exe .\%~n1%~sx1 
 GOTO end
:.rb
 ruby "%~f1"
 GOTO end
:.php
 php "%~f1"
 GOTO end

:end

pause

As a note upgrading to Windows7 and Powershell 2 I found some Issues with this and have updated to passing in an ExecutionPolicy to ensure I can run the script I am editing.

:.ps1
  cd "%~d1%~p1"
  powershell -ExecutionPolicy Unrestricted -File "%~n1%~sx1"
  GOTO end

Problem

I am using Notepad++ to edit a PowerShell file and want to be able to execute that file from inside Notepad++. How can I set that up?

Original source