英文:
Get list of processes with a open window
问题
在PowerShell中,gps | where {$_.mainwindowtitle}
这个命令用于列出所有具有打开窗口的正在运行的进程。您疑惑的部分是,这个命令首先列出所有正在运行的进程,然后根据条件 mainwindowtitle == True
进行筛选,但在gps (get-process)
或 tasklist
的输出中似乎没有名为 "mainwindowtitle" 的列。您是否理解代码的方式有误呢?
您的理解是正确的,gps
实际上是 Get-Process
命令的缩写,用于获取系统中的进程列表。然后,where {$_.mainwindowtitle}
是一个筛选条件,它只返回具有非空 mainwindowtitle
的进程,这意味着这些进程具有打开的窗口。
"mainwindowtitle" 列是 System.Diagnostics.Process
对象的属性,但默认情况下,在输出中并不显示这个属性。只有当您使用 Select-Object
命令来显式选择这个属性时,它才会在输出中显示。
如果您希望查看进程的 "mainwindowtitle" 属性,可以像这样修改命令:
gps | where {$_.mainwindowtitle} | Select-Object ProcessName, MainWindowTitle
这将显示具有打开窗口的进程的名称和窗口标题。
希望这能解答您的疑问。如有任何建议或问题,请随时提出。
英文:
Basically I was searching for solution to list all running processes which have open windows, and one solution was frequently mentioned (in PowerShell):
gps | where {$_.mainwindowtitle}
and I thought, this command listed firstly all running processes and filtered based on criterium mainwindowtitle == True?
However, there is no such a column called mainwindowtitle in the output of either 'gps (get-process)' or 'tasklist' (at least what as output was printed in the terminal) and is it invisible or is the way i understood the code wrong?
Any advice or suggestion will be appreciated.
答案1
得分: 1
以下是代码部分不需要翻译的内容:
The code works as expected, and - thanks to the separation between data and its display representation in PowerShell's object-based pipeline - can easily be adapted to show what you want:
-
The
System.Diagnostics.Process
objects returned byGet-Process
(whose built-in alias isgps
) have a.MainWindowTitle
property thatWhere-Object
(whose built-in aliases arewhere
and?
) filters by. -
However, the
.MainWindowTitle
property values are simply not displayed by default, because the formatting definitions for the output type do not define a display column for it (horizontal space in tables is limited, so only the most important properties are shown.- You can select your own display columns by passing the names of the properties of interest to
Format-Table
(whose built-in alias isft
):
- You can select your own display columns by passing the names of the properties of interest to
# Find all processes that have a main-window title and
# show their process ID, name, and main-window title in a table.
Get-Process |
Where-Object MainWindowTitle |
Format-Table -Property Id, Name, MainWindowTitle
The most concise equivalent of the above, using aliases (recommended for interactive use only):
gps | ? MainWindowTitle | ft Id, Name, MainWindowTitle
Note:
-
Simplified syntax is used in the
Where-Object
call.-
That is,
MainWindowTitle
is equivalent to passing script block{ $_.MainWindowTitle }
-
Even though the
.MainWindowTitle
property contains strings, they are implicitly evaluated as Booleans ($true
or$false
) byWhere-Object
, simply based on whether they are empty ($false
) or not ($true
).
That is,{ $_.MainWindowTitle }
is effectively the same as the more verbose{ $_.MainWindowTitle -ne '' }
For a summary of PowerShell's implicit to-Boolean coercion logic, see the bottom section of this answer.
-
-
It is important to note that
Format-*
cmdlets should only ever be used for display formatting, and never to supply output for subsequent programmatic processing.-
If you want to select a subset of properties as data, for later programmatic processing, use
Select-Object
(whose built-in alias isselect
) instead. -
See this answer for more information.
-
英文:
<!-- language-all: sh -->
The code works as expected, and - thanks to the separation between data and its display representation in PowerShell's object-based pipeline - can easily be adapted to show what you want:
-
The
System.Diagnostics.Process
objects returned byGet-Process
(whose built-in alias isgps
) have a.MainWindowTitle
property thatWhere-Object
(whose built-in aliases arewhere
and?
) filters by. -
However, the
.MainWindowTitle
property values are simply not displayed by default, because the formatting definitions for the output type do not define a display column for it (horizontal space in tables is limited, so only the most important properties are shown.- You can select your own display columns by passing the names of the properties of interest to
Format-Table
(whose built-in alias isft
):
- You can select your own display columns by passing the names of the properties of interest to
# Find all processes that have a main-window title and
# show their process ID, name, and main-window title in a table.
Get-Process |
Where-Object MainWindowTitle |
Format-Table -Property Id, Name, MainWindowTitle
The most concise equivalent of the above, using aliases (recommended for interactive use only):
gps | ? MainWindowTitle | ft Id, Name, MainWindowTitle
Note:
-
Simplified syntax is used in the
Where-Object
call.-
That is,
MainWindowTitle
is equivalent to passing script block{ $_.MainWindowTitle }
-
Even though the
.MainWindowTitle
property contains strings, they are implicitly evaluated as Booleans ($true
or$false
) byWhere-Object
, simply based on whether they are empty ($false
) or not ($true
).
That is,{ $_.MainWindowTitle }
is effectively the same as the more verbose{ $_.MainWindowTitle -ne '' }
For a summary of PowerShell's implicit to-Boolean coercion logic, see the bottom section of this answer.
-
-
It is important to note that
Format-*
cmdlets should only ever be used for display formatting, and never to supply output for subsequent programmatic processing.-
If you want to select a subset of properties as data, for later programmatic processing, use
Select-Object
(whose built-in alias isselect
) instead. -
See this answer for more information.
-
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论