Select-String is probably not the hammer you wanna use for this particular nail (see below) :-)

Get-Process has a -Name parameter that takes a wildcard:

Get-Process -Name nginx
# or
Get-Process -Name *nginx*

To kill the process, either call Kill() directly on the object:

$nginxProcess = Get-Process nginx |Select -First 1
$nginxProcess.Kill()

... or simply pipe the process instances to Stop-Process:

Get-Process -Name nginx |Stop-Process

As you can see, we never actually need to locate or pass the process id - the Process object already has that information embedded in it, and the *-Process cmdlets are designed to work in concert - PowerShell is all about command composition, and this is an example of it.

That being said, Stop-Process is also perfectly capable of killing processes by name alone:

Stop-Process -Name nginx

How did I know the *-Process cmdlets had a -Name parameter?

Apart from reading the help files and documentation (I get it, I don't want to read anything either unless I absolutely have to ;-)), a quick way to learn about the parameters exposed by a cmdlet is by running Get-Command <commandName> -Syntax:

PS ~> Get-Command Stop-Process -Syntax

Stop-Process [-Id] <int[]> [-PassThru] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

Stop-Process -Name <string[]> [-PassThru] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

Stop-Process [-InputObject] <Process[]> [-PassThru] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

The output shows us 3 distinct "parameter sets" (combinations of parameter input accepted by the command), and the required and optional arguments we can pass to it.


What's wrong with Select-String?

The Select-String cmdlet is the PowerShell cognate to grep - it takes some input, and performs regular expression matching against it based on whatever pattern you give it.

But grep is only useful when you're operating on strings - and as you've already found, Get-Process returns structured .NET objects, not flat strings.

Instead, the PowerShell-idiomatic approach is to filter the data, using the Where-Object cmdlet:

Get-Process | Where-Object Name -like '*nginx*'

Here, we instruct Where-Object to only let through object that have a Name property, the value of which must satisfy the wildcard pattern *nginx*.

Where-Object also supports arbitrary filter expressions, by accepting a scriptblock - PowerShell will assign the current pipeline object being evaluated to $_ (and $PSItem):

Get-Process | Where-Object { $_.Name -like '*nginx*' }

... which you can extend to whatever degree you need:

# Only let them through if a specific user is executing
Get-Process | Where-Object { $_.Name -like '*nginx*' -and $env:USERNAME -ne 'Quarkly'}
Answer from Mathias R. Jessen on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.management › stop-process
Stop-Process (Microsoft.PowerShell.Management) - PowerShell | Microsoft Learn
The Stop-Process cmdlet stops one or more running processes. You can specify a process by process name or process ID (PID), or pass a process object to Stop-Process. Stop-Process works only on processes running on the local computer.
Top answer
1 of 2
8

Select-String is probably not the hammer you wanna use for this particular nail (see below) :-)

Get-Process has a -Name parameter that takes a wildcard:

Get-Process -Name nginx
# or
Get-Process -Name *nginx*

To kill the process, either call Kill() directly on the object:

$nginxProcess = Get-Process nginx |Select -First 1
$nginxProcess.Kill()

... or simply pipe the process instances to Stop-Process:

Get-Process -Name nginx |Stop-Process

As you can see, we never actually need to locate or pass the process id - the Process object already has that information embedded in it, and the *-Process cmdlets are designed to work in concert - PowerShell is all about command composition, and this is an example of it.

That being said, Stop-Process is also perfectly capable of killing processes by name alone:

Stop-Process -Name nginx

How did I know the *-Process cmdlets had a -Name parameter?

Apart from reading the help files and documentation (I get it, I don't want to read anything either unless I absolutely have to ;-)), a quick way to learn about the parameters exposed by a cmdlet is by running Get-Command <commandName> -Syntax:

PS ~> Get-Command Stop-Process -Syntax

Stop-Process [-Id] <int[]> [-PassThru] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

Stop-Process -Name <string[]> [-PassThru] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

Stop-Process [-InputObject] <Process[]> [-PassThru] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

The output shows us 3 distinct "parameter sets" (combinations of parameter input accepted by the command), and the required and optional arguments we can pass to it.


What's wrong with Select-String?

The Select-String cmdlet is the PowerShell cognate to grep - it takes some input, and performs regular expression matching against it based on whatever pattern you give it.

But grep is only useful when you're operating on strings - and as you've already found, Get-Process returns structured .NET objects, not flat strings.

Instead, the PowerShell-idiomatic approach is to filter the data, using the Where-Object cmdlet:

Get-Process | Where-Object Name -like '*nginx*'

Here, we instruct Where-Object to only let through object that have a Name property, the value of which must satisfy the wildcard pattern *nginx*.

Where-Object also supports arbitrary filter expressions, by accepting a scriptblock - PowerShell will assign the current pipeline object being evaluated to $_ (and $PSItem):

Get-Process | Where-Object { $_.Name -like '*nginx*' }

... which you can extend to whatever degree you need:

# Only let them through if a specific user is executing
Get-Process | Where-Object { $_.Name -like '*nginx*' -and $env:USERNAME -ne 'Quarkly'}
2 of 2
1

Note: PowerShell must be run as Administrator in order to execute these commands.

Kill a process with a known PID:

Syntax:

Stop-Process -Force -Id <pid>

Example:

Stop-Process -Force -Id 1234

Kill a process with a known name:

Syntax:

Stop-Process -Force -Name <name>

Example:

Stop-Process -Force -Name Taskmgr

Kill a process with a name wildcard search pattern

Syntax:

Get-Process -Name <pattern> | Stop-Process -Force

Example:

Get-Process -Name *skmg* | Stop-Process -Force
🌐
ShellGeek
shellgeek.com › home › powershell tips › powershell stop-process – how to kill process if running
PowerShell Stop-Process - How to Kill Process If Running - ShellGeek
April 14, 2024 - Stop-Process cmdlet stops the process if running. You can kill a process using process name, process Id (PID), not owned by the current user
🌐
Playwright
playwright.dev › command line
Command line | Playwright
Playwright provides a powerful command line interface for running tests, generating code, debugging, and more. The most up to date list of commands and arguments available on the CLI can always be retrieved via npx playwright --help.
Find elsewhere
🌐
Java2Blog
java2blog.com › home › powershell › powershell kill process by name
PowerShell Kill Process by Name [4 Ways] - Java2Blog
June 20, 2023 - In this method, only write the name of the process without extension. For example, do not write "notepad.exe" instead, write "notepad". Use the Terminate() method to kill the process by name in PowerShell.
🌐
Computer Performance
computerperformance.co.uk › home › powershell
PowerShell Scripting Basics: Kill Process | Stop-Process | Code Examples
January 15, 2019 - We can put this knowledge to use by creating a variable to hold the notepad process object, then applying the .kill() method. # PowerShell Kill Process Clear-Host $process = Get-Process notepad $process.Kill()
🌐
How-To Geek
howtogeek.com › home › files › application not responding? here's how to kill processes with powershell
Application Not Responding? Here's How to Kill Processes with PowerShell
April 19, 2012 - Ever have one of those days where programs just aren’t cooperating? You try terminating a program, but it doesn’t respond? PowerShell can give you some extra fire power on those days. 1. The long command name stop-process can be shortened to kill.
🌐
Bitsum
bitsum.com
Bitsum. Real-time CPU Optimization and Automation
Our novel ProBalance algorithm helps maintain system responsiveness during high CPU loads by dynamically adjusting the priorities of running programs to keep problematic background processes in check. With ProBalance, no longer will single, or multiple, processes be able to bring your system to a virtual stall.
🌐
ControlUp Community
controlupcommunity.com › home › what command is executed when you right click on a proces and “kill process”?
What command is executed when you right click on a proces and "kill process"? | ControlUp Community
August 27, 2023 - They needed to create an automated action to kill winlogon.exe, which is preventing sessions from logging out. It was suggested that they use the Powershell Cmdlets for SOLVE Actions, as found in the ControlUp documentation. Alternatively, a stop-process force or taskkill can be used as part ...
🌐
Dzhavat Ushev
dzhavat.github.io › 2020 › 04 › 09 › powershell-script-to-kill-a-process-on-windows.html
PowerShell script to kill a process on Windows | Dzhavat Ushev
This was something completely new to me so I explored a few different options. One of them was to write a PowerShell script that would look for a specific port and kill the process related to it. That way, I reasoned, I can kill the server after each test. But there was one problem - I had ...
🌐
TechBlog
techblog.byllemos.dk › 2019 › 01 › 06 › how-to-kill-tasks-in-powershell
How to kill tasks in PowerShell – TechBlog
October 6, 2019 - To kill a process in the command prompt you use taskkill with parameters, in this example I use PID (Process ID) to kill the notepad process. You could also use flags like IM (Image Name) or FI (Filters).
🌐
Comparitech
comparitech.com › home › net admin › tutorial: powershell kill process command
PowerShell Kill Process Command: Step-by-Step Tutorial
November 11, 2024 - You will learn how to open and navigate the PowerShell environment, get a list of running processes, and use commands like 'Taskkill' and 'Stop-Process' to terminate processes by their ID or name. We will also briefly discuss how to kill a process without PowerShell using the Task Manager.
🌐
PowerShell Forums
forums.powershell.org › powershell help
Kill process of a different user remotely - PowerShell Help - PowerShell Forums
August 25, 2016 - Hi there, I’m just learning the basics of powershell and I wanted to resolve the following issue: I need to kill a specific task that is run on a remote computer by all users that are not currently logged in. I want to be able to run the command on a Windows 7 machine. What I got so far: Get-WmiObject win32_process ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › windows-server › administration › windows-commands › taskkill
taskkill | Microsoft Learn
Ends one or more tasks or processes. Processes can be ended by process ID or image name. You can use the tasklist command command to determine the process ID (PID) for the process to be ended. ... This command replaces the kill tool.
🌐
Microsoft
devblogs.microsoft.com › dev blogs › scripting blog [archived] › use powershell to repeatedly terminate specific processes
Use PowerShell to Repeatedly Terminate Specific Processes - Scripting Blog [archived]
September 19, 2014 - In the command, use the query and specify an action to stop the process. Use the Wait-Event cmdlet to wait until an event triggers the action. Note Today's blog post builds on concepts that I discussed yesterday in Use PowerShell to Monitor Specific Process Creation, so you should review that post first.