Prevent multiple instance of application running using Powershell

Introduction

In production environments, it’s crucial to ensure that applications run smoothly and efficiently. A common problem is managing multiple instances of the same application, which can lead to conflicts, excessive resource usage and unexpected errors. To solve this problem, we can use a PowerShell script that prevents multiple instances of an application from running simultaneously.

Usefulness of the script in production environments

This PowerShell script is particularly useful in production environments for several reasons :

  • Resource optimization : Production environments require efficient resource management. This script helps avoid excessive memory and CPU consumption caused by multiple instances.
  • Improved performance : By ensuring that only a single instance of the application is running, scripting helps maintain optimum performance, which is essential for mission-critical applications in production.
  • Conflict prevention : By preventing multiple instances of an application from running, the script reduces the risk of conflicts between processes, which can improve application stability.
  • Error reduction : Errors caused by multiple instances can be difficult to diagnose and correct. By limiting the application to a single instance, the script simplifies error handling and improves overall reliability.

What exactly does this script do ?

The script continuously monitors the instances of a specific application (process) and ensures that only one instance remains active. If it detects more than one instance, it automatically terminates any additional instances.

You can use this script also with GPO in Active Directory.

Note :

  • This script forces additional processes to close, so make sure no critical data is lost.
  • You can adjust the waiting period by modifying Start-Sleep 900 if necessary (default is 15 min).

PowerShell script :

keep in mind to replace $processName variable with your own process name.

 # Process Name
$processName = "Process Name Here"


while($true)
{
    try
    {
        # Get all process with name = process name
        $AllProcess = Get-Process $processName -ErrorAction SilentlyContinue

        if($AllProcess.count -eq 1)
        {
            Write-Host "Only one Application instance is running...." -ForegroundColor Green
        }
        else
        {
            Write-Host "Total of Process Number is : " $AllProcess.count -ForegroundColor Yellow
            
            # Start Killing other process
            $AllProcess | Select-Object -Skip 1 | ForEach-Object {
            Stop-Process -Id $_.Id -Force
            Write-Host "Application Instance Stopped : $($_.Id)"  -ForegroundColor Yellow
            }
        }
    }
    catch
    {
        Write-Host "Error : Please check process name" -ForegroundColor Red
    }

    # Start sleeping for 15 minutes
    Start-Sleep 900
}

Thanks

Aymen EL JAZIRI (Microsoft MVP)
Aymen EL JAZIRI (Microsoft MVP)

Hi, I’m Aymen El Jaziri , a passionate System Administrator and Microsoft MVP, with years of hands-on experience in managing and securing modern IT infrastructures.
This blog is where I share technical guides, automation scripts, product reviews, and real-world solutions that help IT professionals simplify their day-to-day work and stay ahead in a fast-evolving cloud ecosystem.
Whether you’re here to troubleshoot an issue, improve your automation game, or learn new best practices , welcome in my blog !
Let’s build a stronger, smarter IT community together.
Feel free to connect with me on LinkedIn for more content, discussions, or collaboration opportunities.

Thanks

Aymen

Articles: 154