Skip to content

Instantly share code, notes, and snippets.

@taddison
Created July 13, 2018 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taddison/e2affea7b72c4c75d425eb27d335df05 to your computer and use it in GitHub Desktop.
Save taddison/e2affea7b72c4c75d425eb27d335df05 to your computer and use it in GitHub Desktop.
Restart a windows service using more memory than a given cutoff
#requires -RunAsAdministrator
$serviceName = "BITS"
$cutoffGB = 0.0001
$processId = (Get-WMIObject win32_service | where { $_.name -eq $serviceName}).ProcessID
$process = Get-Process -Id $processId
$workingSet = $process.WorkingSet64
$workingSetGB = $workingSet / 1GB
if($workingSet -gt $cutoffGB) {
Write-Warning "Service using $workingSetGB GB"
Restart-Service -Name $serviceName -Verbose
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment