Prevent multiple instance of application running using Powershell

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

System Administrator

To view or add a comment, sign in

More articles by Aymen E.

Insights from the community

Others also viewed

Explore topics