PowerShell Check If Process Is Running

PowerShell check if process is running

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.

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-Process cmdlet 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".

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 gps or ps aliases are available to execute the Get-Process cmdlet. However, in PowerShell 7, which is cross-platform and runs on Linux, the ps command conflicts with a built-in Linux command. Therefore, the ps command will not work on Linux but the gps alias.

Using Tasklist Command with Where-Object Cmdlet

Use the Tasklist command with the Where-Object cmdlet to check if process is running in PowerShell.

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.

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.

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.

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-CimInstance cmdlet is similar to the Get-WmiObject cmdlet 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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *