Run PowerShell Scripts in Notepad++
Summary
Introduction
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.
Editing the Shortcuts File
Locate the file
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.
Initial steps
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.
If the file exists
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>
If the file does not exist
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.
Edit the <UserDefinedCommands>
section
Inside 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.
Important notes
- About the
-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.
- Avoid shortcut conflicts
Before defining Ctrl+F5 or Ctrl+F6, access Settings > Shortcut Mapper… > Run commands tab and check if these shortcuts are already in use.
If there are conflicts, choose other key combinations, such as
Key="118"
for F7, or edit existing shortcuts in the menu.
- Meaning of the
Key
property values
The number used in Key
corresponds to the virtual key code in Windows:
-
116
represents F5
-
117
represents F6
These codes are how Notepad++ recognizes which key will be used for the shortcut.
- Path variable with spaces or special characters
The expression
"$(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).
Back up and replace the original file
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.
Installing and Configuring the Environment
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
Unzip the contents of the ZIP file into a folder of your choice, for example, C:\Progs\pwsh.
Add 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.
Using the New Shortcuts
With these configurations, three new shortcuts to run your scripts (2 for .ps1
and 1 for .bat
or .cmd
) will be available:
- Ctrl+F5: Will run a
.ps1
script using Windows PowerShell (powershell.exe
).
- Ctrl+F6: Will run a
.ps1
script using PowerShell [cross-platform] (pwsh.exe
).
- Ctrl+F7: Will run a
.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++.
Considerations on Execution Policies
In corporate environments, Group Policies Objects (GPOs) may block -ExecutionPolicy Bypass
. In this case:
Conclusion
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.