How to Uninstall Notepad++ with PowerShell Scripting

This guide provides a thorough explanation on how to uninstall Notepad++ with PowerShell scripting. Managing software across multiple systems is a critical responsibility for IT administrators and Managed Service Providers (MSPs). One common challenge is ensuring that unwanted or outdated applications are completely removed from client machines. Notepad++, though widely used, may occasionally need to be uninstalled for compliance, standardization, or troubleshooting. The PowerShell script provided offers an automated, reliable, and administrator-friendly way to handle this task across Windows environments.

Background

Manual uninstalls often lead to incomplete removals, leftover files, or errors when the application is running. For IT teams managing fleets of devices, doing this individually isn’t feasible. By using PowerShell, administrators can automate the process, enforce consistency, and avoid manual errors. This script focuses specifically on using PowerShell to uninstall Notepad++, handling both MSI and EXE installations, while also accounting for scenarios where the application is still running.

The Script

#Requires -Version 5.1

<#
.SYNOPSIS
    Uninstall Notepad++. If the app is running, and the Force Close Notepad checkbox is unchecked, the uninstall will fail. If the Force Close Notepad is checked, and the app is running, unsaved work will not be saved.
.DESCRIPTION
    Uninstall Notepad++. If the app is running, and the Force Close Notepad checkbox is unchecked, the uninstall will fail. If the Force Close Notepad is checked, and the app is running, unsaved work will not be saved.
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
    Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
.EXAMPLE
    (No Parameters)
    Verifying that Notepad++ is installed.
    Removing Notepad++ using 'C:\Program Files (x86)\Notepad++\uninstall.exe /S'.
    Verifying that Notepad++ has been removed.
    Notepad++ has been successfully removed.

.PARAMETER ForceCloseNotepad
    Verifying that Notepad++ is installed.
    Notepad++ was successfully force closed.
    Removing Notepad++ using 'C:\Program Files\Notepad++\uninstall.exe /S'.
    Verifying that Notepad++ has been removed.
    Notepad++ has been successfully removed.

.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2016
    Version: 1.0
    Release Notes: Initial Release
#>

[CmdletBinding()]
param (
    [Parameter()]
    [Switch]$ForceCloseNotepad = [System.Convert]::ToBoolean(${env:forceCloseNotepad++})
)

begin {
    # Check if the operating system build version is less than 10240 (Windows 10 or Windows Server 2016 minimum requirement)
    if ([System.Environment]::OSVersion.Version.Build -lt 10240) {
        Write-Host -Object "`n[Warning] The minimum OS version supported by this script is Windows 10 (10240) or Windows Server 2016 (14393)."
        Write-Host -Object "[Warning] OS build '$([System.Environment]::OSVersion.Version.Build)' detected. This could lead to errors or unexpected results.`n"
    }

    function Find-InstallKey {
        [CmdletBinding()]
        param (
            [Parameter(ValueFromPipeline = $True)]
            [String]$DisplayName,
            [Parameter()]
            [Switch]$UninstallString
        )
        process {
            # Initialize a list to store found installation keys
            $InstallList = New-Object System.Collections.Generic.List[Object]

            # Search in the standard HKLM paths
            # Search in the 32-bit uninstall registry key and add results to the list
            try {
                $Result = Get-ChildItem -Path "Registry::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" | Get-ItemProperty | Where-Object { $_.DisplayName -like "*$DisplayName*" }
                if ($Result) { $InstallList.Add($Result) }
            } catch {
                Write-Host -Object "[Warning] Failed to retrieve registry keys at 'HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'."
                throw $_
            }

            # Search in the 64-bit uninstall registry key and add results to the list
            try {
                $Result = Get-ChildItem -Path "Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Get-ItemProperty | Where-Object { $_.DisplayName -like "*$DisplayName*" }
                if ($Result) { $InstallList.Add($Result) }
            } catch {
                Write-Host -Object "[Warning] Failed to retrieve registry keys at 'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'."
                throw $_
            }
            # If the UninstallString switch is set, return only the UninstallString property of the found keys
            if ($UninstallString) {
                $InstallList | Select-Object -ExpandProperty UninstallString -ErrorAction SilentlyContinue
            } else {
                $InstallList
            }
        }
    }

    function Test-IsElevated {
        [CmdletBinding()]
        param ()

        # Get the current Windows identity of the user running the script
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()

        # Create a WindowsPrincipal object based on the current identity
        $p = New-Object System.Security.Principal.WindowsPrincipal($id)

        # Check if the current user is in the Administrator role
        # The function returns $True if the user has administrative privileges, $False otherwise
        # 544 is the value for the Built In Administrators role
        # Reference: https://learn.microsoft.com/en-us/dotnet/api/system.security.principal.windowsbuiltinrole
        $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]'544')
    }

    function Wait-ForNotepad {
        [CmdletBinding()]
        param()

        $Start = Get-Date
        $Timeout = [TimeSpan]::FromMinutes(5)

        if (Get-Process -Name "notepad++" -ErrorAction SilentlyContinue) {
            Write-Host -Object "Waiting for the Notepad++ process to stop."
        }

        while (Get-Process -Name "notepad++" -ErrorAction SilentlyContinue) {
            if (((Get-Date) - $Start) -ge $Timeout) {
                Write-Host -Object "[Warning] The five minute timeout has been reached."
                break
            }
            Start-Sleep -Milliseconds 100
        }
    }

    if (!$ExitCode) {
        $ExitCode = 0
    }
}
process {
    # Attempt to determine if the current session is running with Administrator privileges.
    try {
        $IsElevated = Test-IsElevated -ErrorAction Stop
    } catch {
        Write-Host -Object "[Error] $($_.Exception.Message)"
        Write-Host -Object "[Error] Unable to determine if the account '$env:Username' is running with Administrator privileges."
        exit 1
    }

    if (!$IsElevated) {
        Write-Host -Object "[Error] Access Denied: The user '$env:Username' does not have administrator privileges, or the script is not running with elevated permissions."
        exit 1
    }

    # Check if Notepad++ is still installed by searching uninstall registry keys
    Write-Host -Object "Verifying that Notepad++ is installed."
    try {
        $NotepadInstalled = Find-InstallKey -DisplayName "Notepad++" -UninstallString -ErrorAction Stop
    } catch {
        # Output error if unable to verify installation
        Write-Host -Object "[Error] $($_.Exception.Message)"
        Write-Host -Object "[Error] Failed to verify whether Notepad++ is installed."
        exit 1
    }

    # Check for Notepad++ uninstaller in Program Files (x86)
    if (Test-Path -Path "${env:ProgramFiles(x86)}\Notepad++\uninstall.exe" -PathType Leaf -ErrorAction SilentlyContinue) {
        $NotepadUninstallerFound = $True
    }

    # Check for Notepad++ uninstaller in Program Files
    if (Test-Path -Path "${env:ProgramFiles}\Notepad++\uninstall.exe" -PathType Leaf -ErrorAction SilentlyContinue) {
        $NotepadUninstallerFound = $True
    }

    # Check if Notepad++ is currently running
    $NotepadRunning = Get-Process -Name "notepad++" -ErrorAction SilentlyContinue

    # If Notepad++ is running and force close is selected, attempt to close the application before uninstalling.
    if ($ForceCloseNotepad -and $NotepadRunning){
        Write-Host -Object "Found Notepad++ running, attempting to close it."
        try {
            Stop-Process -Name "notepad++" -Force
            Write-Host -Object "Notepad++ was successfully force closed."
        } catch {
            Write-Host -Object "[Error] $($_.Exception.Message)"
            Write-Host -Object "[Error] Unable to force close Notepad++"
            exit 1
        }
    }

    # If force close option is not selected and notepad++ running and installed, exit.
    if (!$ForceCloseNotepad -and $NotepadRunning -and ($NotepadInstalled -or $NotepadUninstallerFound)) {
        Write-Host -Object "[Error] Unable to uninstall Notepad++ because it is currently in use. Attempt to rerun the script with the Force Close Notepad checkbox enabled."
        exit 1
    }

    # If neither registry nor uninstaller found, Notepad is not installed
    if (!$NotepadInstalled -and !$NotepadUninstallerFound) {
        if ($NotepadRunning) {
            Write-Host -Object "[Alert] The version of Notepad++ running on this system is the portable edition."
        }
        Write-Host -Object "[Error] Notepad++ is not installed on this system."
        exit 1
    }

    # If Notepad++ was installed via MSI, uninstall using MsiExec
    if ($NotepadInstalled -match "MsiExec\.exe /I") {
        $NotepadInstalled | ForEach-Object {
            # Prepare MSI uninstall arguments
            $MsiExecArguments = @(
                ($_ -replace "^MsiExec\.exe " -replace "/I", "/x")
                "/qn"
                "/norestart"
            )

            try {
                Write-Host -Object "Removing Notepad++ using '$env:WINDIR\System32\MsiExec.exe $($MsiExecArguments -join " ")'."
                # Start MSI uninstall process
                $UninstallProcess = Start-Process -FilePath "$env:WINDIR\System32\MsiExec.exe" -ArgumentList $MsiExecArguments -NoNewWindow -Wait -PassThru -ErrorAction Stop
                if ($UninstallProcess.ExitCode -ne 0) {
                    Write-Host -Object "[Warning] The last exit code '$($UninstallProcess.ExitCode)' does not indicate success."
                }
            } catch {
                # Output error if MSI uninstall fails
                Write-Host -Object "[Error] Exit Code: $($UninstallProcess.ExitCode)"
                Write-Host -Object "[Error] $($_.Exception.Message)"
                Write-Host -Object "[Error] Failed to uninstall Notepad++."
                $ExitCode = 1
            }

            try {
                # Wait for Notepad++ processes to exit
                Wait-ForNotepad -ErrorAction Stop
            } catch {
                Write-Host -Object "[Error] $($_.Exception.Message)"
                Write-Host -Object "[Error] Failed to wait for the Notepad++ uninstaller process to complete."
                $ExitCode = 1
            }
        }
    }

    # If Notepad++ was installed with the exe, run the uninstaller
    if ($NotepadInstalled -match "uninstall\.exe") {
        $NotepadInstalled | ForEach-Object {
            # Extract the uninstall.exe path from the uninstall string
            $UninstallString = $_ -split '"([^"]+)"'
            $FilePath = $UninstallString | Where-Object { $_ -match "uninstall\.exe" }

            # Prepare silent uninstall arguments
            $UninstallArguments = @(
                "/S"
            )

            try {
                Write-Host -Object "Removing Notepad++ using '$FilePath $($UninstallArguments -join " ")'."
                # Start uninstall process
                $UninstallProcess = Start-Process -FilePath $FilePath -ArgumentList $UninstallArguments -NoNewWindow -Wait -PassThru -ErrorAction Stop
                if ($UninstallProcess.ExitCode -ne 0) {
                    Write-Host -Object "[Warning] The last exit code '$($UninstallProcess.ExitCode)' does not indicate success."
                }
            } catch {
                # Output error if uninstall.exe fails
                Write-Host -Object "[Error] Exit Code: $($UninstallProcess.ExitCode)"
                Write-Host -Object "[Error] $($_.Exception.Message)"
                Write-Host -Object "[Error] Failed to uninstall Notepad++."
                $ExitCode = 1
            }

            try {
                # Wait for Notepad++ processes to exit
                Wait-ForNotepad -ErrorAction Stop
            } catch {
                Write-Host -Object "[Error] $($_.Exception.Message)"
                Write-Host -Object "[Error] Failed to wait for the Notepad++ uninstaller process to complete."
                $ExitCode = 1
            }
        }
    }

    # Fallback: If uninstall.exe exists in Program Files (x86), run it directly
    if (Test-Path -Path "${env:ProgramFiles(x86)}\Notepad++\uninstall.exe" -PathType Leaf -ErrorAction SilentlyContinue) {
        try {
            Write-Host -Object "Removing Notepad++ using '${env:ProgramFiles(x86)}\Notepad++\uninstall.exe /S'."
            $UninstallProcess = Start-Process -FilePath "${env:ProgramFiles(x86)}\Notepad++\uninstall.exe" -ArgumentList "/S" -NoNewWindow -PassThru -ErrorAction Stop
            if ($UninstallProcess.ExitCode -ne 0) {
                Write-Host -Object "[Warning] The last exit code '$($UninstallProcess.ExitCode)' does not indicate success."
            }
        } catch {
            # Output error if uninstall.exe fails
            Write-Host -Object "[Error] Exit Code: $($UninstallProcess.ExitCode)"
            Write-Host -Object "[Error] $($_.Exception.Message)"
            Write-Host -Object "[Error] Failed to uninstall Notepad++."
            $ExitCode = 1
        }

        try {
            # Wait for Notepad++ processes to exit
            Wait-ForNotepad -ErrorAction Stop
        } catch {
            Write-Host -Object "[Error] $($_.Exception.Message)"
            Write-Host -Object "[Error] Failed to wait for the Notepad++ uninstaller process to complete."
            $ExitCode = 1
        }
    }

    # Fallback: If uninstall.exe exists in Program Files, run it directly
    if (Test-Path -Path "${env:ProgramFiles}\Notepad++\uninstall.exe" -PathType Leaf -ErrorAction SilentlyContinue) {
        try {
            Write-Host -Object "Removing Notepad++ using '${env:ProgramFiles}\Notepad++\uninstall.exe /S'."
            $UninstallProcess = Start-Process -FilePath "${env:ProgramFiles}\Notepad++\uninstall.exe" -ArgumentList "/S" -NoNewWindow -PassThru -ErrorAction Stop
            if ($($UninstallProcess.ExitCode) -ne 0) {
                Write-Host -Object "[Warning] The last exit code '$($UninstallProcess.ExitCode)' does not indicate success."
            }
        } catch {
            # Output error if uninstall.exe fails
            Write-Host -Object "[Error] Exit Code: $($UninstallProcess.ExitCode)"
            Write-Host -Object "[Error] $($_.Exception.Message)"
            Write-Host -Object "[Error] Failed to uninstall Notepad++."
            $ExitCode = 1
        }

        try {
            # Wait for Notepad++ processes to exit
            Wait-ForNotepad -ErrorAction Stop
        } catch {
            Write-Host -Object "[Error] $($_.Exception.Message)"
            Write-Host -Object "[Error] Failed to wait for the Notepad++ uninstaller process to complete."
            $ExitCode = 1
        }
    }

    # Final verification that Notepad++ has been removed
    Write-Host -Object "Verifying that Notepad++ has been removed."
    try {
        $NotepadInstalled = Find-InstallKey -DisplayName "Notepad++" -UninstallString -ErrorAction Stop
    } catch {
        # Output error if unable to verify removal
        Write-Host -Object "[Error] $($_.Exception.Message)"
        Write-Host -Object "[Error] Failed to verify whether Notepad++ is still installed."
        exit 1
    }

    # Check for notepad++.exe in both Program Files location, it tends to stay if the application was uninstalled without closing the app first.
    if (Test-Path -Path "${env:ProgramFiles(x86)}\Notepad++\notepad++.exe" -PathType Leaf -ErrorAction SilentlyContinue) {
        $NotepadUninstallerFoundAgain = $True
    }

    if (Test-Path -Path "${env:ProgramFiles}\Notepad++\notepad++.exe" -PathType Leaf -ErrorAction SilentlyContinue) {
        $NotepadUninstallerFoundAgain = $True
    }

    # If neither registry nor uninstaller found, removal was successful
    if (!$NotepadInstalled -and !$NotepadUninstallerFoundAgain) {
        Write-Host -Object "Notepad++ has been successfully removed."
    } else {
        Write-Host -Object "[Error] Notepad++ is still installed."
        exit 1
    }

    exit $ExitCode
}
end {
    
    
    
}

 

Detailed Breakdown

The script has several core functions and logic paths that make it versatile:

  1. Environment Validation
    • Checks whether the OS build supports the script (Windows 10 or Server 2016 and later).
    • Verifies administrator privileges before proceeding.
  2. Registry and File Checks
    • Uses a custom Find-InstallKey function to locate Notepad++ uninstall entries in both 32-bit and 64-bit registry locations.
    • Searches for uninstall.exe in both Program Files and Program Files (x86) directories.
  3. Process Handling
    • Determines if Notepad++ is running.
    • If the ForceCloseNotepad parameter is set, it will forcibly terminate the process. If not, the uninstall fails gracefully with an error.
  4. Uninstallation Logic
    • If installed via MSI, the script calls MsiExec with silent uninstall flags.
    • If installed via EXE, it executes the uninstall.exe /S command.
    • Includes fallback mechanisms to run uninstallers directly if registry entries are missing.
  5. Verification
    • After running the uninstall, the script verifies removal by re-checking registry entries and file paths.
    • Provides clear messages for success or failure.

Potential Use Cases

Imagine a company that recently decided to standardize on Visual Studio Code. Notepad++ must be removed from all endpoints. Using this script, an MSP can push the script via NinjaOne across hundreds of systems. If some users still have Notepad++ open, the ForceCloseNotepad option ensures removal still occurs, though IT teams must weigh the risk of unsaved work being lost.

Comparisons

Other ways to remove Notepad++ include:

  • Manual uninstall via Control Panel or Settings, which doesn’t scale well.
  • Group Policy or Intune uninstall commands, which work but require more setup and lack robust verification.
  • Third-party uninstaller tools, which may add overhead and licensing costs.

This PowerShell script is lightweight, fully auditable, and integrates seamlessly with automation platforms like NinjaOne.

Implications

Automating application removal has significant implications for IT security and compliance. Outdated or unauthorized applications can introduce vulnerabilities. Ensuring clean, automated removal minimizes attack surfaces, enforces policy, and reduces IT overhead.

Recommendations

  • Always test the script in a controlled environment before mass deployment.
  • Use the ForceCloseNotepad switch only when necessary, since it risks data loss.
  • Run the script within a deployment tool like NinjaOne to ensure consistency and logging.
  • Document and communicate with users if software removal may impact workflows.

Final Thoughts

This PowerShell script provides IT teams with a reliable method to uninstall Notepad++ across multiple machines. It accounts for different installation methods, handles running processes, and validates completion. When integrated with a management platform like NinjaOne, administrators can automate software lifecycle management at scale—saving time, reducing errors, and ensuring systems remain compliant and secure.

FAQs

If ForceCloseNotepad is enabled, unsaved work will be lost.

No, portable editions don’t register uninstall keys. The script alerts you if a portable version is running.

The script accounts for typical paths but relies on registry entries for accuracy. Non-standard paths may require modifications.

Next Steps

Building an efficient and effective IT team requires a centralized solution that acts as your core service delivery tool. NinjaOne enables IT teams to monitor, manage, secure, and support all their devices, wherever they are, without the need for complex on-premises infrastructure.

Learn more about NinjaOne Remote Script Deployment, check out a live tour, or start your free trial of the NinjaOne platform.

Categories:

You might also like