Of course, when you’re working with system, you are going to be doing a lot of analysis and poking around with the processes that are running on the system.

And no matter what the OS is, there are already built in tools for working with those processes. But with PowerShell, you have a couple of cmdlets that are made for working with processes, making it easy to start, stop and administer the processes that are running on your machine.

So do you need PowerShell to do this? Well, in a way, you don’t. But what PowerShell does is bundle up all of the automation and functionality AROUND the process management. Filtering processes in a list, and then taking appropriate actions on just the processes that you care about (or are bugging you (warning – OLD post link alert 🙂

But I want to make sure that if you are new to PowerShell, that you can continue to build on your skills of working with the commands, and that you can work with processes in a familiar way to other PowerShell commands. We’ll walk you through each step with clear explanations and code snippets. Remember, learning new skills and practicing regularly are essential for mastering PowerShell.

Setting up Your PowerShell Environment

In this article I assume that you’re working with PowerShell 7 or later, and that you’re using Visual Studio Code as your editor. This is a great, cross platform approach and you should be able to follow along with everything from that setup. You should also have the PowerShell extension installed in VS Code. If you need any help you can read installation instructions for PowerShell on Linux, Mac or Windows.

Viewing Processes and Process Information in PowerShell

You’re going to want to start any exploration into processes with looking at what’s already running. This is very easy with PowerShell. Just use the Get-Process cmdlet.

For example, to list all running processes:

Get-Process

To filter processes by name or ID, use the -Name or -Id parameters, respectively:

Get-Process -Name "notepad"
Get-Process -Id 1234

Starting New Processes with PowerShell

To start processes, use the Start-Process cmdlet. For example, to start Notepad:

Start-Process "notepad.exe"

To start a process with arguments and a specific working directory:

Start-Process "cmd.exe" -ArgumentList "/c dir" -WorkingDirectory "C:\temp"

To run a process in the background or with elevated privileges, use the -NoNewWindow or -Verb parameters:

Start-Process "powershell.exe" -NoNewWindow -ArgumentList "-Command Get-Process"
Start-Process "powershell.exe" -Verb "RunAs" -ArgumentList "-Command Get-Process"

Use PowerShell to Stop Running Processes

To stop processes, use the Stop-Process cmdlet. For example, to stop a process by its ID:

Stop-Process -Id 1234

Or you could stop processes by their name:

Stop-Process -Name "notepad"

And if you need to apply a little more pressure to forcibly stop the process, you can use the -Force parameter:

Stop-Process -Name "notepad" -Force

How to Monitor Processes with PowerShell

Alright, here we get into something a little less “basic”. You might have a need to query a little more information about a running process, and PowerShell is great at helping you to do that.

To monitor processes, use the Get-Counter cmdlet. For example, to monitor the CPU and memory usage of a process:

Get-Counter -Counter "\Process(notepad)\% Processor Time", "\Process(notepad)\Working Set"

I cover some more pieces of process monitoring and working with counters and events in Troubleshooting with PowerShell: Event Logs, Performance Counters, and More. Taking approaches to elevate your understanding of the processes running on the system and really digging into the OS level is definitely a deep topic but one that can help you to really establish yourself as an expert at your job.

Automating Process Management Tasks with PowerShell

Of course, PowerShell is all about automation and management. So you can definitely use this to your advantage when you’re included these operations in your scripts and functions.

Combining tasks in a script allows you to automate process management tasks. For example, to start a process, monitor its resource usage, and stop it after a certain threshold is reached:

$process = Start-Process "notepad.exe" -PassThru
$threshold = 1000000

while ($true) {
    $memoryUsage = (Get-Process -Id $process.Id).WorkingSet
    if ($memoryUsage -gt $threshold) {
        Stop-Process -Id $process.Id
        break
    }
    Start-Sleep -Seconds 5
}

If you like to use PowerShell to automate system resources, you should check out another (warning: another OLD article alert!) 😁How to Setup a PowerShell Script to Run as a Scheduled Task

Best Practices and General Tips for Managing Processes with PowerShell

  • Use the -WhatIf parameter to preview the result of a command before executing it, helping to prevent unintentional changes or data loss.
  • Utilize variables and pipelines to store data and pass it between cmdlets, creating more efficient and flexible scripts.
  • Automate repetitive tasks by creating scripts that perform complex process management operations, saving time and reducing the possibility of human error.

Continuing your PowerShell journey

As you continue learning PowerShell, practice and apply your new skills to real-world scenarios. Seek out a mentor or enroll in a course to deepen your understanding of PowerShell. Explore additional resources, such as the official PowerShell documentation and community forums, to learn about new cmdlets and techniques.

Wrap Up

In this article, we covered the basics of process management using PowerShell, including viewing, starting, stopping, and monitoring processes. Keep practicing and applying these skills to become more proficient in using PowerShell for various tasks. PowerShell is a powerful tool that can help you automate tasks, manage systems, and troubleshoot issues, so continue exploring and building your skills – it’s going to make you a better professional as you include more DevOps concepts into your daily work!