Run Powershell Scripts in Notepad++

Lai, Shi Peng 0 Reputation points
2023-05-11T20:02:20.7433333+00:00

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....

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,970 questions
{count} votes

4 answers

Sort by: Oldest
  1. Rich Matheisen 47,886 Reputation points
    2023-05-12T01:53:56.2033333+00:00

    @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

    0 comments No comments

  2. CrackerJack 5 Reputation points
    2023-07-24T22:13:13.8333333+00:00

    To answer the question, for anyone looking.

    pwsh "$(FULL_CURRENT_PATH)"

    1 person found this answer helpful.
    0 comments No comments

  3. Laird Mark D. MacLachlan 11 Reputation points
    2023-10-12T22:58:34.7066667+00:00

    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.

    0 comments No comments

  4. João Mac-Cormick 0 Reputation points
    2025-06-08T04:10:53.62+00:00

    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&amp;werShell (powershell.exe)" FolderName="E&amp;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] (&amp;pwsh.exe)" FolderName="E&amp;xecute in terminal" Ctrl="yes" Alt="no" Shift="no" Key="117">pwsh -ExecutionPolicy Bypass -NoExit -File "$(FULL_CURRENT_PATH)"</Command>
        <Command name="Command Prompt (&amp;cmd.exe)" FolderName="E&amp;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

    1. 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.
    2. 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.
    3. 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.
    4. 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%:

    Download PowerShell [cross-platform]

    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.


    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.