有了Powershell,我怎样才能得到一个特定用户拥有的进程的ID?

使用Powershell,我想要做相当于Unix

ps -auxc | grep Emacs kill -9 364 

(假设365是第一个命令告诉我的pid)

我怎么做? 我发现很难让Powershell告诉我一个进程的所有者和我尝试的方式,得到一个wmi对象的win32_process,然后把它放到一个名为“explorer.exe”的进程列表中,只是启动了大量的explorer.exe实例。

我用google快速find了这个:

 $owners = @{} gwmi win32_process |% {$owners[$_.handle] = $_.getowner().user} get-process | select processname,Id,@{l="Owner";e={$owners[$_.id.tostring()]}} 

似乎按要求返回用户。 你可以从那里出发。 希望有所帮助。

使用PowerShell 4和更新版本,您可以执行以下操作:

 Get-Process -IncludeUserName | Where-Object {$_.UserName -match "peter" -and $_.ProcessName -eq "Notepad"} | Stop-Process -Force 

或使用别名时:

 ps -IncludeUserName | ? {$_.UserName -match "peter" -and $_.ProcessName -eq "Notepad"} | spps -Force 

不像Unix那么短,但比旧的PowerShell版本更好。