{"id":1189,"date":"2021-07-17T04:38:28","date_gmt":"2021-07-17T04:38:28","guid":{"rendered":"https:\/\/www.velaninfo.com\/rs\/?post_type=techtips&#038;p=1189"},"modified":"2021-07-17T04:38:28","modified_gmt":"2021-07-17T04:38:28","slug":"powershell-restart-app-process","status":"publish","type":"techtips","link":"https:\/\/www.velaninfo.com\/rs\/techtips\/powershell-restart-app-process\/","title":{"rendered":"How to Use PowerShell to Restart a Crashed or Closed App\/Process?"},"content":{"rendered":"<p>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).<\/p>\n<p><em>If (!(Get-Process -Name notepad -ErrorActionSilentlyContinue))<\/em><\/p>\n<p><em>{Invoke-Item C:\\Windows\\notepad.exe<\/em><\/p>\n<p><em>}<\/em><\/p>\n<p>If a process doesn&#8217;t reply (hangs) or starts to use too much memory (over 1000 MB in this case), you can have it immediately restarted:<\/p>\n<p><em>$proc = Get-Process -Name notepad| Sort-Object -Property ProcessName -Unique<\/em><\/p>\n<p><em>If (($proc.Responding -eq $false) \u2013or ($proc.WorkingSet -GT 1000000*1024)} {<\/em><\/p>\n<p><em>$proc.Kill()<\/em><\/p>\n<p><em>Start-Sleep -s 10<\/em><\/p>\n<p><em>Invoke-Item C:\\Windows\\notepad.exe<\/em><\/p>\n<p><em>}<\/em><\/p>\n<p>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:<\/p>\n<p>for(;;){<\/p>\n<p>try{<\/p>\n<p>If (!(Get-Process -Name notepad -ErrorActionSilentlyContinue))<\/p>\n<p>{Invoke-Item C:\\Windows\\notepad.exe}<\/p>\n<p>$proc = Get-Process -Name notepad | Sort-Object -Property ProcessName -Unique -ErrorActionSilentlyContinue<\/p>\n<p>If (!$proc -or ($proc.Responding -eq $false) \u2013or ($proc.WorkingSet -GT 200000*1024)) {<\/p>\n<p>$proc.Kill()<\/p>\n<p>Start-Sleep -s 10<\/p>\n<p>Invoke-Item C:\\Windows\\notepad.exe}<\/p>\n<p>}<\/p>\n<p>catch\u00a0\u00a0\u00a0 {\u00a0\u00a0\u00a0 }<\/p>\n<p>Start-sleep -s 60<\/p>\n<p>}<\/p>\n<p>This command can be used to verify the status of a process on a remote computer:<\/p>\n<p><em>$proc = Get-Process -ComputerNameSRV1-VISPL211 -Name notepad | Sort-Object -Property ProcessName -Unique -ErrorActionSilentlyContinue<\/em><\/p>\n<p>You can use the Invoke-Command cmdlet to start a process from remote:<\/p>\n<p><em>Invoke-Command -ComputerNameSRV1-VISPL211 -Credential $Cred -ScriptBlock {Start-Process C:\\Windows\\notepad.exe -wait -verb runas;}<\/em><\/p>\n<p>This PowerShell script can be used as a GPO logon script at user logon.<\/p>\n<p>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 \u2013ExecutionPolicy Bypass option to launch the script.<\/p>\n<p>File name: %windir%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe<\/p>\n<p>Running options: -windowstyle hidden -ExecutionPolicy Bypass \u2013Noprofile -file %~dp0CheckProcess.ps1<\/p>\n<p>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.<\/p>\n<p><em>$Action= New-ScheduledTaskAction -Execute &#8220;PowerShell.exe&#8221; -Argument &#8220;-windowstyle hidden -ExecutionPolicy Bypass -file %windir%\\CheckProcess.ps1&#8221;<\/em><\/p>\n<p><em>$Trigger= New-ScheduledTaskTrigger -AtLogon<\/em><\/p>\n<p><em>$Principal=New-ScheduledTaskPrincipal -UserId &#8220;jsmith&#8221; -LogonType Interactive<\/em><\/p>\n<p><em>$Task=New-ScheduledTask -Action $Action -Trigger $Trigger -Principal $Principal<\/em><\/p>\n<p><em>Register-ScheduledTask -TaskName &#8220;Check Notepad Process&#8221; -InputObject $Task<\/em><\/p>\n<p>At Velan, our server support engineers can manage your server.\u00a0If you are interested in our service, please fill the Quick connect form to <a href=\"https:\/\/www.velaninfo.com\/contact\"><strong>get in touch with us<\/strong><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<a class=\"continue-reading text-uppercase\" href=\"https:\/\/www.velaninfo.com\/rs\/techtips\/powershell-restart-app-process\/\"> Continue Reading <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.velaninfo.com\/rs\/wp-content\/themes\/velaninfo\/images\/reading_arw.png\" alt=\"Continue Reading\" width=\"16\" height=\"12\"\/><\/a><\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"tags":[],"class_list":["post-1189","techtips","type-techtips","status-publish","hentry","Categories_tech_tip-core-server-windows","Categories_tech_tip-powershell","Categories_tech_tip-server-windows","Categories_tech_tip-windows"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v19.5 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to use PowerShell to Restart Apps on Windows | Velan<\/title>\n<meta name=\"description\" content=\"If you\u2019re familiar with the Windows PowerShell terminal, you can use it to force quit apps on Windows using the taskkill command.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.velaninfo.com\/rs\/techtips\/powershell-restart-app-process\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use PowerShell to Restart a Crashed or Closed App\/Process?\" \/>\n<meta property=\"og:description\" content=\"If you\u2019re familiar with the Windows PowerShell terminal, you can use it to force quit apps on Windows using the taskkill command.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.velaninfo.com\/rs\/techtips\/powershell-restart-app-process\/\" \/>\n<meta property=\"og:site_name\" content=\"Velan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.velaninfo.com\\\/rs\\\/techtips\\\/powershell-restart-app-process\\\/\",\"url\":\"https:\\\/\\\/www.velaninfo.com\\\/rs\\\/techtips\\\/powershell-restart-app-process\\\/\",\"name\":\"How to use PowerShell to Restart Apps on Windows | Velan\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.velaninfo.com\\\/rs\\\/#website\"},\"datePublished\":\"2021-07-17T04:38:28+00:00\",\"description\":\"If you\u2019re familiar with the Windows PowerShell terminal, you can use it to force quit apps on Windows using the taskkill command.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.velaninfo.com\\\/rs\\\/techtips\\\/powershell-restart-app-process\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.velaninfo.com\\\/rs\\\/techtips\\\/powershell-restart-app-process\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.velaninfo.com\\\/rs\\\/techtips\\\/powershell-restart-app-process\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.velaninfo.com\\\/rs\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tech Tips\",\"item\":\"https:\\\/\\\/www.velaninfo.com\\\/rs\\\/techtips\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Use PowerShell to Restart a Crashed or Closed App\\\/Process?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.velaninfo.com\\\/rs\\\/#website\",\"url\":\"https:\\\/\\\/www.velaninfo.com\\\/rs\\\/\",\"name\":\"Velan\",\"description\":\"Velaninfo Services India Pvt Ltd\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.velaninfo.com\\\/rs\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to use PowerShell to Restart Apps on Windows | Velan","description":"If you\u2019re familiar with the Windows PowerShell terminal, you can use it to force quit apps on Windows using the taskkill command.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.velaninfo.com\/rs\/techtips\/powershell-restart-app-process\/","og_locale":"en_US","og_type":"article","og_title":"How to Use PowerShell to Restart a Crashed or Closed App\/Process?","og_description":"If you\u2019re familiar with the Windows PowerShell terminal, you can use it to force quit apps on Windows using the taskkill command.","og_url":"https:\/\/www.velaninfo.com\/rs\/techtips\/powershell-restart-app-process\/","og_site_name":"Velan","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.velaninfo.com\/rs\/techtips\/powershell-restart-app-process\/","url":"https:\/\/www.velaninfo.com\/rs\/techtips\/powershell-restart-app-process\/","name":"How to use PowerShell to Restart Apps on Windows | Velan","isPartOf":{"@id":"https:\/\/www.velaninfo.com\/rs\/#website"},"datePublished":"2021-07-17T04:38:28+00:00","description":"If you\u2019re familiar with the Windows PowerShell terminal, you can use it to force quit apps on Windows using the taskkill command.","breadcrumb":{"@id":"https:\/\/www.velaninfo.com\/rs\/techtips\/powershell-restart-app-process\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.velaninfo.com\/rs\/techtips\/powershell-restart-app-process\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.velaninfo.com\/rs\/techtips\/powershell-restart-app-process\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.velaninfo.com\/rs\/"},{"@type":"ListItem","position":2,"name":"Tech Tips","item":"https:\/\/www.velaninfo.com\/rs\/techtips\/"},{"@type":"ListItem","position":3,"name":"How to Use PowerShell to Restart a Crashed or Closed App\/Process?"}]},{"@type":"WebSite","@id":"https:\/\/www.velaninfo.com\/rs\/#website","url":"https:\/\/www.velaninfo.com\/rs\/","name":"Velan","description":"Velaninfo Services India Pvt Ltd","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.velaninfo.com\/rs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.velaninfo.com\/rs\/wp-json\/wp\/v2\/techtips\/1189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.velaninfo.com\/rs\/wp-json\/wp\/v2\/techtips"}],"about":[{"href":"https:\/\/www.velaninfo.com\/rs\/wp-json\/wp\/v2\/types\/techtips"}],"author":[{"embeddable":true,"href":"https:\/\/www.velaninfo.com\/rs\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.velaninfo.com\/rs\/wp-json\/wp\/v2\/comments?post=1189"}],"version-history":[{"count":1,"href":"https:\/\/www.velaninfo.com\/rs\/wp-json\/wp\/v2\/techtips\/1189\/revisions"}],"predecessor-version":[{"id":1190,"href":"https:\/\/www.velaninfo.com\/rs\/wp-json\/wp\/v2\/techtips\/1189\/revisions\/1190"}],"wp:attachment":[{"href":"https:\/\/www.velaninfo.com\/rs\/wp-json\/wp\/v2\/media?parent=1189"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.velaninfo.com\/rs\/wp-json\/wp\/v2\/tags?post=1189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}