Arrays PowerShell杀死多个进程

Arrays PowerShell杀死多个进程,arrays,powershell,process,terminate,Arrays,Powershell,Process,Terminate,我正试图通过PS完成以下任务,但在获得我需要的东西时遇到了问题。我已经尝试了许多不同的格式来编写这个脚本,这是我认为最接近的 我运行了以下命令,没有错误,但也没有结果 $softwarelist = 'chrome|firefox|iexplore|opera' get-process | Where-Object {$_.ProcessName -eq $softwarelist} | stop-process -force $softwarelist='chrome | firefox |

我正试图通过PS完成以下任务,但在获得我需要的东西时遇到了问题。我已经尝试了许多不同的格式来编写这个脚本,这是我认为最接近的

我运行了以下命令,没有错误,但也没有结果

$softwarelist = 'chrome|firefox|iexplore|opera' get-process | Where-Object {$_.ProcessName -eq $softwarelist} | stop-process -force $softwarelist='chrome | firefox | iexplore | opera' 获取过程| 其中对象{$\进程名-eq$softwarelist}| 停止进程-强制 这里是我尝试的另一个示例,但这个示例并没有终止我提供的所有进程(即在W8.1中)

1..50 |%{记事本;计算器} $null=获取WmiObject win32_进程-筛选器“名称='notepad.exe'或名称='calc.exe'”| %{$\终止()}
谢谢你的帮助

您的
$softwarelist
变量看起来像一个正则表达式,但在
Where Object
条件中,您使用的是
-eq
运算符。我想您需要
-match
操作符:

$softwarelist = 'chrome|firefox|iexplore|opera'
get-process |
    Where-Object {$_.ProcessName -match $softwarelist} |
    stop-process -force
您还可以将多个进程传递给
获取进程
,例如

Get-Process -Name 'chrome','firefox','iexplore','opera' | Stop-Process -Force

我尝试了一下,用-match代替了-eq。你简化了字符串,这是一个改进,我将使用它。-eq不能以这种方式与变量数组一起使用吗?我不确定
-eq
对数组的行为,但在这种情况下
$softwarelist
不是数组,而是字符串。要创建一个数组,您必须将其拆分,例如
$softwarelist-split'|'
。我喜欢使用多个数组的想法,我觉得数组=灵活性。也许这是我在DBA工作时的错误想法。
Get-Process -Name 'chrome','firefox','iexplore','opera' | Stop-Process -Force
# First, create an array of strings.
$array =  @("chrome","firefox","iexplore","opera")


# Next, loop through each item in your array, and stop the process.
foreach ($process in $array)
{
    Stop-Process -Name $process
}