Powershell Kill all processes except system -
in powershell, kill processes users, except explorer , processes used system
this including errors given:
$cred = get-credential; invoke-command -computername localhost -credential $cred -scriptblock { get-process $env:allusersprofile | where-object -filterscript {$_.name -ne "system, network service, local service"} | where-object -filterscript {$_.name -ne "explorer"} | stop-process -whatif } cannot find process name "c:\programdata". verify process name , call cmdlet again. + categoryinfo : objectnotfound: (c:\programdata:string) [get-process], processcommandexception + fullyqualifiederrorid : noprocessfoundforgivenname,microsoft.powershell.commands.getprocesscommand + pscomputername : localhost
here, should work you.
function stop-userprocesses{ param([string]$computer = "localhost") $cred = get-credential invoke-command -computername $computer -credential $cred -scriptblock { get-process -includeusername | where{!($_.username -match "nt authority\\(?:system|(?:local|network) service)") -and !($_.processname -eq "explorer")}|stop-process -whatif } }
once convinced functional remove -whatif. call stop-userprocesses
end locally, or stop-userprocesses somecomputer01
end on remote system (assuming have remote sessions enabled in environment).
edit: then, evidently -includeusername switch new in v4. so, in order want have jump through hoops , use get-wmiobject on win32_process class, execute getowner() method each process. want filter don't end things idle throwing errors when don't have owner, we'll make sure commandline property exists.
function stop-userprocesses{ param([string]$computer = "localhost") $cred = get-credential invoke-command -computername $computer -credential $cred -scriptblock { #get processes $processes = get-wmiobject win32_process|where{![string]::isnullorempty($_.commandline)}|select *,@{l='owner';e={$_.getowner().user}} #filter out system , service processes $processes = $processes | { !($_.owner -match "(?:system|(?:local|network) service)") } #get processes , filter on process id , name = explorer, pipe stop-process get-process | { $processes.processid -contains $_.id -and $_.name -ne "explorer" } | stop-process -whatif } }
Comments
Post a Comment