14

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?

5 Answers 5

8

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this answer - this makes it simple and aweseome! Notepad++ has long been a favorite of mine and now it's even better. (Language agnostic + Markdown support + great plugins)
5

You can run a saved script from "Run" -> "Run" menu in Notepad++ with the following command:

powershell.exe -noexit -command . \"$(FULL_CURRENT_PATH)\"

2 Comments

I do not remember why I used -command. But you're right, I might as well have used -file which would have made the full command cleaner.
Thought there was a reason for it, well, apparently not... :D
3

I would recommend using PowerShell ISE which comes as part of PowerShell and designed specifically for Powershell.

1 Comment

ps ise is good , but it locks the file and does not have nice npp features - search, highlight, etc
2

See Using Notepad++ to Compile and Run Java Programs and replace "javac" with "C:Windows\system32\WindowsPowerShell\v1.0\powershell.exe" (or your path to PowerShell). (Caveat: I'm not a Notepad++ user and haven't tried this.)

That said, I'd just use PowerShell ISE (installs with PowerShell) or one of the other dedicated PowerShell IDEs instead.

2 Comments

I agree... you can do it, but it's likely easier and more productive to just keep a powershell shell, an ise, and notepad++ open at all times. I write and test on the command line, transfer and edit in notepad++, run the script on the command line, then, if need be, use the ISE to debug. I rarely end up using the ISE though.
PowerShell is in the PATH, so no need to actually give the complete path, I guess.
2

Based on the answers before:

powershell.exe -ExecutionPolicy Unrestricted -NoLogo -File "$(FULL_CURRENT_PATH)"

You can also add the -NoExit parameter to keep PowerShell from closing automatically:

powershell.exe -ExecutionPolicy Unrestricted -NoExit -NoLogo -File "$(FULL_CURRENT_PATH)"

Note: File has to be saved.

Comments

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.