How to Use PowerShell to Restart a Crashed or Closed App/Process? Core

Learn how to use PowerShell to check if a given application or process is running, as well as how to automatically resume it in the event of a crash, a user closing it by accident, or it consuming a lot of memory (memory leak).

If (!(Get-Process -Name notepad -ErrorActionSilentlyContinue))

{Invoke-Item C:\Windows\notepad.exe

}

If a process doesn’t reply (hangs) or starts to use too much memory (over 1000 MB in this case), you can have it immediately restarted:

$proc = Get-Process -Name notepad| Sort-Object -Property ProcessName -Unique

If (($proc.Responding -eq $false) –or ($proc.WorkingSet -GT 1000000*1024)} {

$proc.Kill()

Start-Sleep -s 10

Invoke-Item C:\Windows\notepad.exe

}

You can create an endless loop using PowerShell for loop that launches a process, checks if it is running every 60 seconds, and restarts it if necessary:

for(;;){

try{

If (!(Get-Process -Name notepad -ErrorActionSilentlyContinue))

{Invoke-Item C:\Windows\notepad.exe}

$proc = Get-Process -Name notepad | Sort-Object -Property ProcessName -Unique -ErrorActionSilentlyContinue

If (!$proc -or ($proc.Responding -eq $false) –or ($proc.WorkingSet -GT 200000*1024)) {

$proc.Kill()

Start-Sleep -s 10

Invoke-Item C:\Windows\notepad.exe}

}

catch    {    }

Start-sleep -s 60

}

This command can be used to verify the status of a process on a remote computer:

$proc = Get-Process -ComputerNameSRV1-VISPL211 -Name notepad | Sort-Object -Property ProcessName -Unique -ErrorActionSilentlyContinue

You can use the Invoke-Command cmdlet to start a process from remote:

Invoke-Command -ComputerNameSRV1-VISPL211 -Credential $Cred -ScriptBlock {Start-Process C:\Windows\notepad.exe -wait -verb runas;}

This PowerShell script can be used as a GPO logon script at user logon.

The PowerShell code should then be saved as a *.PS1 file. You can use a digital signature to sign the script, update the PowerShell Execution policy settings, or use the –ExecutionPolicy Bypass option to launch the script.

File name: %windir%\System32\WindowsPowerShell\v1.0\powershell.exe

Running options: -windowstyle hidden -ExecutionPolicy Bypass –Noprofile -file %~dp0CheckProcess.ps1

The Task Scheduler can also be used to run a PS1 script on a regular basis. The same run options should be used. You can optionally select a user account for the process to operate under.

$Action= New-ScheduledTaskAction -Execute “PowerShell.exe” -Argument “-windowstyle hidden -ExecutionPolicy Bypass -file %windir%\CheckProcess.ps1”

$Trigger= New-ScheduledTaskTrigger -AtLogon

$Principal=New-ScheduledTaskPrincipal -UserId “jsmith” -LogonType Interactive

$Task=New-ScheduledTask -Action $Action -Trigger $Trigger -Principal $Principal

Register-ScheduledTask -TaskName “Check Notepad Process” -InputObject $Task

At Velan, our server support engineers can manage your server. If you are interested in our service, please fill the Quick connect form to get in touch with us.

Credentials

Quick Connect With Us


    captcha
    reload