PowerShell : Processes

There are five core Process cmdlets, designed to managing processes.

  • Get-Process
  • Start-Process
  • Stop-Process
  • Wait-Process
  • Debug-Process

you can get the above by using Get-Help \*-Process, and you can find information about each Service cmdlet by using Get-Help .

Get-Process

The Get-Service cmdlet gets processes that are running on the local computer.

Get a list of all active processes

PS C:\> Get-Process

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    237      38     2724       9108       0.41   9500   3 adb
    324      16    18668      22428   1,130.66   2796   0 audiodg
    178      11     3980       6480              3548   0 ChgService
    271      24    31356      47796       1.02    920   3 chrome
    276      33    76680      87796       1.69   2020   3 chrome
....................
..................................

Get all available data about of a processes

PS C:\> Get-Process explorer | Format-List *

Name                       : explorer
Id                         : 11244
PriorityClass              : Normal
FileVersion                : 10.0.17134.1 (WinBuild.160101.0800)
HandleCount                : 2127
WorkingSet                 : 113254400
PagedMemorySize            : 53190656
......................
................................

Get all processes with a working set greater than a specified size

PS C:\> Get-Process | `
Where-Object {$_.WorkingSet -gt 20000000}

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    324      16    18668      22420   1,130.66   2796   0 audiodg
    271      24    31356      47796       1.02    920   3 chrome
    273      33    76656      87808       1.69   2020   3 chrome
    335      48   137144     165980       9.06   2260   3 chrome
    376      45   160516     191416      13.30   2676   3 chrome
    268      21    18308      30572       0.25   2888   3 chrome
............
.......................

List processes on the computer in groups based on priority

PS C:\> Get-Process  | Format-Table -View priority

   PriorityClass: Normal

ProcessName                  Id   HandleCount WorkingSet64
-----------                  --   ----------- ------------
adb                        9500           237      9330688
audiodg                    2796           324     22958080
ChgService                 3548           178      6615040
chrome                      920           271     48943104


   PriorityClass: Idle

ProcessName                  Id   HandleCount WorkingSet64
-----------                  --   ----------- ------------
chrome                     2020           273     89915392
..................
.........................

Get version information for a process

PS C:\> Get-Process chrome -FileVersionInfo

ProductVersion   FileVersion      FileName
--------------   -----------      --------
71.0.3578.98     71.0.3578.98     C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

Get all the processes that have the main window title and display them in a table

PS C:\> Get-Process | `
where {$_.mainWindowTitle} | `
Format-Table id, name, mainwindowtitle `
-autosize

   Id Name             MainWindowTitle
   -- ----             ---------------
11880 chrome           PowerShell : Processes – Get Start - Google Chrome
10196 editplus         Untitled1 * - EditPlus
 5676 powershell       Windows PowerShell
10432 SwUSB

Start-Process

The Start-Process cmdlet starts one or more processes on the local computer.

Start a process that uses default values

PS C:\> Start-Process -FilePath "sort.exe"

Print a text file

PS C:\> Start-Process -FilePath "hello.txt" `
-WorkingDirectory "$HOME\Desktop" `
-Verb Print

Start a process to sort items to a new file

PS C:\> Start-Process -FilePath "Sort.exe" `
-RedirectStandardInput "$HOME/Desktop/sort.txt" `
-RedirectStandardOutput "$HOME/Desktop/sorted.txt" `
-RedirectStandardError "$HOME/Desktop/sorterror.txt" `
-UseNewEnvironment

Start a process in a maximized window

PS C:\>Start-Process -FilePath "notepad" `
-Wait -WindowStyle Maximized

Start PowerShell as an administrator

PS C:\> Start-Process -FilePath "powershell" -Verb runAs

Starts a PowerShell process in a new console window

PS C:\> Start-Process -FilePath "powershell.exe" -Verb open

Stop-Process

The Stop-Process cmdlet stops one or more processes on the local computer.

Stop all instances of a process

PS C:\> Stop-Process -Name "notepad"

Stop a specific instance of a process

PS C:\> Stop-Process -Id 3952 -Confirm -PassThru

The Confirm parameter directs PowerShell to prompt you before it stops the process.

Stop a process not owned by the current user

PS C:\> Get-Process -Name "lsass" | Stop-Process -Force

Wait-Process

The Wait-Process cmdlet waits for a process to be stopped before accepting more input.

Stop a process and wait

PS C:\> Stop-Process -Name "notepad"
PS C:\> Wait-Process -Name "notepad"

Wait for processes for a specified time

PS C:\> Wait-Process -Name outlook -Timeout 30

Debug-Process

The Debug-Process cmdlet debugs one or more processes running on the local computer.

Attach a debugger to a process on the computer

PS C:\> Debug-Process "Winlogon"

Attach a debugger to multiple processes

PS C:\> Debug-Process "Explorer", "Outlook"

 

Advertisements

Learn Technology

%d bloggers like this: