Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a PowerShell script from Notepad++

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?

like image 471
runxc1 Bret Ferrier Avatar asked Dec 08 '10 23:12

runxc1 Bret Ferrier


People also ask

How do I make a ps1 file executable?

To convert a single PowerShell script to EXE via the command-line requires a single line providing the main PS2EXE command ( Invoke-PS2EXE ) followed by the script's path to convert and the path to the EXE you'd like to create. You can now run target.exe, and it will invoke the code defined in the source.


2 Answers

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
like image 88
runxc1 Bret Ferrier Avatar answered Oct 15 '22 15:10

runxc1 Bret Ferrier


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.

like image 37
TrueWill Avatar answered Oct 15 '22 13:10

TrueWill