To answer the question, for anyone looking.
pwsh "$(FULL_CURRENT_PATH)"
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
I would like to execute/run PowerShell Scripts from Notepad++
See article below,
https://itecnote.com/tecnote/r-how-to-execute-a-powershell-script-from-notepad/
Is this something that can be achieved ? Or it's not supported....
To answer the question, for anyone looking.
pwsh "$(FULL_CURRENT_PATH)"
@Dannelly Jr, Robert B Those videos were originally on Microsofts' "Channel9". They're still relevant, but also out of date (beside being overly long because of the back-and-forth between the presenters).
This is more up-to-date (but still not to PS 5) and you can move at your own pace: https://www.sapien.com/books_training/Windows-PowerShell-4
I would like to jump in and say I fully understand why you might want to execute a script from within NotePad++.
For anyone advocating using PowerShell_ISE which is installed on all servers by default I agree it is the simplest solution, however there are drawbacks to doing so. There is a known bug that will cause the ISE to lock up if your script creates a GUI form. I have seen this bug activate when using code to create login prompts for MSGraphAPI. Microsoft has ignored this bug and is directing us to use VS Code. The issue with that is that it is a larger install and my company does not want to push that to our servers.
The security folks at my company advocate switching to PWSH, and some modules such as PNP.PowerShell are making the move to PowerShell 7+. The issue with that is that not everything supports it. For example we use Atlassian Bamboo to schedule jobs. Bamboo only supports running script files for PowerShell.exe not PWSH.exe. While I certainly could create a bamboo job to launch PWSH.exe and pass it a script name, that would result in the Bamboo logs only showing me errors in launching the script and not the script log that I would otherwise get.
Microsoft was wise to allow multiple versions of PowerShell to exist on the same machine, however I feel like they made our lives very difficult by not making all versions execute from the same executable name. There is no reason I can understand why they could not replace PowerShell.exe to launch PowerShell files of all versions. Then give us an IDE for the different version and have that ship with the base server code.
To run PowerShell scripts directly from Notepad++, the solution must involve customizing shortcuts. While CrackerJack's answer of pwsh "$(FULL_CURRENT_PATH)"
works, it's possible to create a more robust solution to support both Windows PowerShell (powershell.exe
) and PowerShell [cross-platform] (pwsh.exe
).
The term PowerShell [cross-platform] is used in this document editorially to distinguish the
pwsh.exe
version from the native Windows version (powershell.exe
).The "cross-platform" expression in brackets is not part of the official product name but serves to help the reader understand the differences between the versions.
Notepad++ allows the creation and management of custom shortcuts. Although the Run menu offers a temporary option for commands, they only become permanent if saved in the shortcuts.xml
file.
This solution uses native Notepad++ features, avoiding plugins to ensure lightweight setup and compatibility in restrictive environments.
To locate the shortcuts.xml
file, type %APPDATA%\Notepad++
in the Run window (Win+R) or the File Explorer address bar to open the folder, if Notepad++ was installed for the current user.
Note: If the installation was for all users, check
%ProgramFiles%\Notepad++
. In portable versions, the file is in the program's folder.
Warning: Make sure Notepad++ is not running, including in the background or minimized to the taskbar, before copying the original file. No Notepad++ instance should be open.
Copy the shortcuts.xml
file to your desktop. This is crucial to avoid permission issues and data loss, especially in protected folders like %ProgramFiles%
or %APPDATA%\Notepad++
in environments with security restrictions. Open the shortcuts.xml
file on your desktop with Notepad++. Find a section called <UserDefinedCommands>
. It will look something like this:
<UserDefinedCommands>
<Command name="Get PHP help" Ctrl="no" Alt="yes" Shift="no" Key="112">https://www.php.net/$(CURRENT_WORD)</Command>
<Command name="Wikipedia Search" Ctrl="no" Alt="yes" Shift="no" Key="114">https://en.wikipedia.org/wiki/Special:Search?search=$(CURRENT_WORD)</Command>
<Command name="Open selected file path in new instance" Ctrl="no" Alt="yes" Shift="no" Key="117">$(NPP_FULL_FILE_PATH) $(CURRENT_WORD) -nosession -multiInst</Command>
</UserDefinedCommands>
Using Notepad++, create a file with the basic structure <NotepadPlus><UserDefinedCommands></UserDefinedCommands></NotepadPlus>
on your desktop. When saving it, ensure you use the .xml
extension (e.g., prevent the file from being saved as shortcuts.xml.txt
), and UTF-8 encoding (Encoding > UTF-8) to ensure compatibility.
<UserDefinedCommands>
sectionInside this <UserDefinedCommands>
section, add the following lines to create the shortcuts:
<Command name="Windows Po&werShell (powershell.exe)" FolderName="E&xecute in terminal" Ctrl="yes" Alt="no" Shift="no" Key="116">powershell -ExecutionPolicy Bypass -NoExit -File "$(FULL_CURRENT_PATH)"</Command>
<Command name="PowerShell [cross-platform] (&pwsh.exe)" FolderName="E&xecute in terminal" Ctrl="yes" Alt="no" Shift="no" Key="117">pwsh -ExecutionPolicy Bypass -NoExit -File "$(FULL_CURRENT_PATH)"</Command>
<Command name="Command Prompt (&cmd.exe)" FolderName="E&xecute in terminal" Ctrl="yes" Alt="no" Shift="no" Key="118">cmd /k "$(FULL_CURRENT_PATH)"</Command>
Then, save the .xml
file.
These lines create the shortcuts Ctrl+F5 for Windows PowerShell (powershell.exe
) (Ctrl="yes" Key="116"
) and Ctrl+F6 for PowerShell [cross-platform] (pwsh.exe
) (Ctrl="yes" Key="117"
).
For more details on the
shortcuts.xml
file, consult the Notepad++ documentation.
-NoExit
parameter
It keeps the PowerShell window open after the script execution — useful for debugging and checking results. To close the window manually, just type exit
.
If you prefer the window to close automatically when the script finishes, remove the -NoExit
parameter from the command line.Key="118"
for F7, or edit existing shortcuts in the menu.Key
property values
The number used in Key
corresponds to the virtual key code in Windows:
116
represents F5117
represents F6"$(FULL_CURRENT_PATH)"
is an internal Notepad++ variable that represents the full path of the current script. The quotes ("
) are essential to prevent errors when the path contains spaces or special characters (like accents).Make sure Notepad++ is closed again, rename the original shortcuts.xml
file in the Notepad++ folder (e.g., to shortcuts.original.xml
) to keep a backup, and copy the edited file from the desktop to the source folder.
Note: If Notepad++ was installed in
%ProgramFiles%\Notepad++
, administrative permissions may be required. Run File Explorer as an administrator. In portable versions, check if the program folder has write permissions, especially on USB devices or restricted folders.
Windows PowerShell (powershell.exe
) is native to the Windows operating system, while PowerShell [cross-platform] (pwsh.exe
) is an open-source version that must be downloaded and installed separately. There is also a portable version. For the Ctrl+F6 shortcut to work, the pwsh.exe
executable needs to be accessible through your %PATH%
environment variable. If you don't have pwsh.exe
installed or configured in %PATH%
:
Try running pwsh --version
in the terminal (Command Prompt or Windows PowerShell) to check if pwsh
is already installed. If the pwsh
command is not recognized, this indicates that PowerShell [cross-platform] is not installed or configured in PATH
. In this case, I recommend downloading the latest LTS (Long Term Support) version for greater stability and long-term support, or, if you wish, another specific version. Access the PowerShell GitHub releases page and look for the corresponding .zip
installation file (e.g., PowerShell-X.Y.Z-win-x64.zip). This is the standalone version (also known as "no installer") and should be used instead of the .msi
installer.
Unzip the contents of the ZIP file into a folder of your choice, for example, C:\Progs\pwsh.
pwsh.exe
to %PATH%
Add the C:\Progs\pwsh
folder (or your previously chosen path) to the user's %PATH%
. You can use Windows PowerShell for this:
Open a Windows PowerShell terminal and run the following script. It will check if the path already exists before adding it:
$AddPath = 'C:\Progs\pwsh' # Change to your path if different
$Path = [Environment]::GetEnvironmentVariable('PATH', 'User')
$ArrayOfPath = $Path -split ';'
If ($AddPath.Replace('\', '/') -in $ArrayOfPath.Replace('\', '/')) {
Write-Warning -Message "The path '$AddPath' is already in the user's PATH variable."
} Else {
[Environment]::SetEnvironmentVariable('Path', "$Path;$AddPath", 'User')
Write-Host "The path '$AddPath' was successfully added to the user's PATH variable."
}
Note: The comparison uses the
Replace()
method to normalize paths, as a difference between "" and "/" could cause a false negative, even if the paths are equivalent for the operating system.
With these configurations, three new shortcuts to run your scripts (2 for .ps1
and 1 for .bat
or .cmd
) will be available:
.ps1
script using Windows PowerShell (powershell.exe
)..ps1
script using PowerShell [cross-platform] (pwsh.exe
)..bat
or .cmd
script using Command Prompt (cmd.exe
).Since powershell.exe
is native to Windows and usually in your %PATH%
, the Ctrl+F5 shortcut should work without issues.
In Notepad++, open a PowerShell script with a .ps1
extension, press Ctrl+F5 or Ctrl+F6, and check if the script runs. If the shortcuts don't work, check the shortcuts.xml
file for formatting errors (like missing XML tags) or shortcut conflicts in the Settings > Shortcut Mapper… > Run commands tab.
Note: The command will always run on the file that is focus in Notepad++.
In corporate environments, Group Policies Objects (GPOs) may block -ExecutionPolicy Bypass
. In this case:
-ExecutionPolicy RemoteSigned
for digitally signed scripts. For more details, visit the Set-AuthenticodeSignature page.This solution avoids plugin installation, keeping Notepad++ lightweight and compatible with restrictive corporate environments.
It's a useful option for those seeking simplicity and who don't want to rely on full IDEs like VS Code or PowerShell ISE — or the PowerShell [cross-platform] (pwsh.exe
) terminal itself, if installed.