Table of Contents
Using Get-Process Cmdlet
Use the Get-Process cmdlet to check if process is running in PowerShell. With the Get-Process cmdlet, we can display the list of processes running on the system.
|
1 2 3 |
Get-Process |
|
1 2 3 4 5 6 7 8 9 10 11 |
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 470 26 19184 32148 0.38 4464 7 ApplicationFrameHost 618 44 28960 1496 0.97 10964 7 CalculatorApp 404 21 93572 146764 11.17 1800 7 chrome 247 18 19176 32068 0.11 1816 7 chrome 139 7 1576 8944 0.08 14540 7 CompPkgSrv 166 11 6956 10184 0.19 11416 7 conhost .... |
We can observe the Get-Process cmdlet retrieved all the information of the process currently running on my system. From the above list, we only want ProcessName; it shows the name of the processes. If you want to know about the other columns, check this documentation.
The
Get-Processcmdlet output is the same for Linux and Windows operating systems.
Now, let’s dive into more details. This was the one way to see the list of all the running processes. There are several ways to check if a particular process is running in PowerShell. Once the process is identified, various actions can be performed on it.
Consider a scenario where you want to check if chrome is running on your system; if yes, then display the message "Chrome is running" on a system; otherwise, display "Chrome is not running".
|
1 2 3 4 5 6 7 8 |
#Check if a process named "chrome" is running if (Get-Process -Name chrome -ErrorAction SilentlyContinue) { Write-Host "Chrome is running." } else { Write-Host "Chrome is not running." } |
|
1 2 3 |
Chrome is running. |
In this example, the Get-Process cmdlet is used with the -Name parameter to specify the task name, which is set to chrome to check its status, if it is running or not. Of course, you can replace chrome with the process name you want to check. Here, the -ErrorAction SilentlyContinue parameter is optional; it is used to avoid errors from being displayed if the given process is not running.
By default, the
gpsorpsaliases are available to execute theGet-Processcmdlet. However, in PowerShell 7, which is cross-platform and runs on Linux, thepscommand conflicts with a built-inLinuxcommand. Therefore, thepscommand will not work on Linux but thegpsalias.
Using Tasklist Command with Where-Object Cmdlet
Use the Tasklist command with the Where-Object cmdlet to check if process is running in PowerShell.
|
1 2 3 4 5 6 7 8 |
#Check if a process named "notepad" is running if (Tasklist | Where-Object { $_.ProcessName -eq "notepad" }) { Write-Host "Notepad is running." } else { Write-Host "Notepad is not running." } |
|
1 2 3 |
Notepad is running. |
In this example, the Tasklist command retrieves the list of processes running on the system, which pipes the output with the Where-Object cmdlet.
In PowerShell, the Where-Object cmdlet filters objects based on a specified condition. In this case, it filters the Tasklist output and looks for the process whose ProcessName property is equal to notepad.
Observe in the above example the process is found and "Notepad is running." message is displayed on the screen.
Further reading:
Using Get-WmiObject Cmdlet with Win32_Process Class
Use the Get-WmiObject cmdlet with the Win32_Process class to check if process is running in PowerShell.
|
1 2 3 4 5 6 7 8 |
#Check if a process named "firefox" is running if (Get-WmiObject Win32_Process | Where-Object {$_.Name -eq "firefox.exe"}) { Write-Host "Firefox is running." } else { Write-Host "Firefox is not running." } |
|
1 2 3 |
Firefox is not running. |
The above PowerShell code used the Get-WmiObject cmdlet to retrieve the information about the running processes using the Win32_Process WMI (Windows Management Instrumentation) class. The Win32_Process class represents a process on a Windows operating system. Then, the Where-Object cmdlet filters the collection based on the Name property to retrieve only those processes with firefox.exe in their name.
Using Get-CimInstance Cmdlet with Win32_Process Class
Use the Get-CimInstance cmdlet with the Win32_Process class to check if process is running in PowerShell.
|
1 2 3 4 5 6 7 8 |
#Check if a process named "GoogleDriveFS" is running if (Get-CimInstance Win32_Process | Where-Object {$_ -like "*GoogleDriveFS*"}){ Write-Host "GoogleDriveFS is running." } else { Write-Host "GoogleDriveFS is not running." } |
|
1 2 3 |
GoogleDriveFS is running. |
This example is similar to the above example. Here, the Get-CimInstance cmdlet is used with the Win32_Process class to get information about the running processes. Similar to the above example, the Where-Object cmdlet filters the running process list based on the given condition. In the above case, it filtered the process, which include GoogleDriveFS in its name.
The
Get-CimInstancecmdlet is similar to theGet-WmiObjectcmdlet but uses the more modern WS-Man (Web Services for Management) protocol to communicate. This makes it more efficient and enables cross-platform management of resources.