Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / How to Automatically Restart Crashed or Closed App/Process with PowerShell?

June 16, 2021 PowerShellWindows 10Windows Server 2016

How to Automatically Restart Crashed or Closed App/Process with PowerShell?

Let’s learn how to use PowerShell in order to check if a specific application or process is running; how to restart it automatically in case of a crash, if a user closed it accidentally, or it starts consuming a large amount of memory (memory leak).


Earlier we showed how to manage Windows processes using PowerShell. To make sure if the notepad.exe process is running and restart it, you can use the script below:

If (!(Get-Process -Name notepad -ErrorAction SilentlyContinue))
{Invoke-Item C:\Windows\notepad.exe
}

You can automatically restart a process if it doesn’t respond (is hanging) or if it started to use too much memory (over 1000 MB in this example):

$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
}

Using PowerShell for loop, you can create an endless loop that starts a process, checks every 60 seconds if it is running, and restarts it if needed:

for(;;){
try{
If (!(Get-Process -Name notepad -ErrorAction SilentlyContinue))
{Invoke-Item C:\Windows\notepad.exe}
$proc = Get-Process -Name notepad | Sort-Object -Property ProcessName -Unique -ErrorAction SilentlyContinue
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
}

Powershell Check Proccess is Running

If you want to check the status of a process on remote computer, you can use this command:

$proc = Get-Process -ComputerName WKS-NYC211 -Name notepad | Sort-Object -Property ProcessName -Unique -ErrorAction SilentlyContinue

To start a process remotely, you can use the Invoke-Command cmdlet:

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

You can run this PowerShell script as a GPO logon script at user logon.

Then save the PowerShell code to a file with the *.PS1 extension . You can sign the script with a digital signature, change the PowerShell Execution policy settings, or run the script with the –ExecutionPolicy Bypass option.

  • File name: %windir%\System32\WindowsPowerShell\v1.0\powershell.exe
  • Running options: -windowstyle hidden -ExecutionPolicy Bypass –Noprofile -file %~dp0CheckProcess.ps1

You can also run a PS1 script on schedule using the Task Scheduler. Use the same run options. You can also specify a user account you want to run the process as.

$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

Or you can run this PowerShell script as a Windows service.

If the running app doesn’t need to interact with a user, it is better to run it as a service. Later, you will be able to manage it via the standard services.msc console or with PowerShell. Windows has a built-in feature to restart services, or you can restart a hung up service as follows.

1 comment
0
Facebook Twitter Google + Pinterest
previous post
How to Disable “Open File – Security Warnings” on Windows 10?
next post
Whitelist Domains and Email Addresses on Exchange Server and Microsoft 365

Related Reading

How to Connect VPN Before Windows Logon

November 14, 2023

Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

November 7, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

1 comment

Shlomi July 6, 2021 - 9:33 pm

Amazing!! thank you

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Configuring Port Forwarding in Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • Adding Drivers into VMWare ESXi Installation Image
  • How to Hide Installed Programs in Windows 10 and 11
  • Configuring SFTP (SSH FTP) Server on Windows
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top