All Articles

Managing processes in PowerShell with Get-Process and Stop-Process

This page has been machine-translated from the original page.

Display running processes with the Get-Process cmdlet

In PowerShell, you can use the Get-Process command to retrieve a list of running processes.

With Get-Process, you can obtain parameters such as the process name and process ID, as shown below.

The aliases for Get-Process are gps and ps.
It is convenient that it can be used in a Linux-like way.

PS C:\Users\yuki> Get-Process

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    393      23    16348      30068       0.19   7836   2 ApplicationFrameHost
    569      28    65592      75100   5,603.98  10060   0 audiodg
    308      35    33364      82540       1.48   1308   2 Code

In addition, with Get-Process you can specify which process to display by using the process ID or process name, as shown below.
This is normally used to obtain a known running process, so an error occurs if no matching process is found.

# Specify a process by process name
PS C:\Users\yuki> Get-Process -Name code

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    821      44    38760      87176      12.59   1364   3 Code
    245      21    15188      27940       0.19   4216   3 Code
    531      82   158264     180296      37.78   4984   3 Code
    657      29   180360     197764       8.73   7232   3 Code
    400      51    43332      70696      13.98  10328   3 Code
    438      20     9688      25920       1.11  10820   3 Code
    311      35    33060      51808       1.48  11996   3 Code
    227      16     6896      14048       0.09  14204   3 Code

# Specify a process by ID
PS C:\Users\yuki> Get-Process -Id 4216
Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    245      21    15188      27948       0.19   4216   3 Code

By the way, you can also use wildcards in the process-name search like this.
In this example, specifying note* successfully extracts the notepad process.

PS C:\Users\yuki> Get-Process -Name note*

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    290      17     4068      18728       0.17   4504   3 notepad

Stop a specific process with the Stop-Process command

You can also use the Stop-Process command to stop a specific process by specifying the process name or process ID.
The aliases for Stop-Process are kill and spps.

The kill command is the same as in Bash, so it is very easy to use.

In the following example, Stop-Process is used to stop the notepad process.

# Confirm that the notepad process is running
PS C:\Users\yuki> Get-Process -Name note*

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    290      17     4068      18728       0.17   4504   3 notepad

# Stop the notepad process
PS C:\Users\yuki> Stop-Process -Name notepad

# When you check for the notepad process again, an error is returned because the process is no longer found
PS C:\Users\yuki> Get-Process -Name notepad
python -q
echo  行:1 文字:1
+ Get-Process -Name notepad
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (notepad:String) [Get-Process], ProcessCommandException
    + FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand

You can also perform more complex processing by using command output in this filter portion.

For example, the official documentation introduces the following one-liner for stopping all processes in a “Not Responding” state.

Get-Process | Where-Object -FilterScript {$_.Responding -eq $false} | Stop-Process

It is pretty convenient to be able to quickly stop non-responsive processes.
You no longer have to visually search for them one by one in Task Manager.

Summary

This time, I summarized the Get-Process command for checking PowerShell processes and the Stop-Process command for stopping any process.
Many PowerShell commands are assigned the same aliases as familiar Bash commands, and I find them very comfortable to use.

References

Commands introduced this time

Name
    Get-Process

Syntax
    Get-Process [[-Name] <string[]>]  [<CommonParameters>]

    Get-Process [[-Name] <string[]>]  [<CommonParameters>]

    Get-Process  [<CommonParameters>]

    Get-Process  [<CommonParameters>]

    Get-Process  [<CommonParameters>]

    Get-Process  [<CommonParameters>]


Aliases
    gps
    ps
Name
    Stop-Process

Syntax
    Stop-Process [-Id] <int[]>  [<CommonParameters>]

    Stop-Process  [<CommonParameters>]

    Stop-Process [-InputObject] <Process[]>  [<CommonParameters>]


Aliases
    spps
    kill