Windows Powershell Where-Object 条件过滤
过滤管道结果
使用get-process返回所有的当前进程 ,但是你可能并不对所有的进程感兴趣,然后通过每个process对象的属性进行过滤。首先得知道每个对象支持那些属性。
ps c:powershell> get-process | select -first 1 | fl *
__nounname : process
name : acrord32
handles : 287
vm : 234819584
ws : 32616448
pm : 63488000
npm : 14584
path : c:program filesadobereader 10.0readeracrord32
.exe
company : adobe systems incorporated
cpu : 96.5334188
fileversion : 10.1.2.45
productversion : 10.1.2.45
description : adobe reader
product : adobe reader
id : 4820
priorityclass : normal
handlecount : 287
workingset : 32616448
pagedmemorysize : 63488000
privatememorysize : 63488000
virtualmemorysize : 234819584
totalprocessortime : 00:01:36.5334188
basepriority : 8
exitcode :
hasexited : false
exittime :
handle : 3568
machinename : .
mainwindowhandle : 198686
mainwindowtitle : mastering powershell - adobe reader
mainmodule : system.diagnostics.processmodule (acrord32.exe)
maxworkingset : 1413120
minworkingset : 204800
modules : {system.diagnostics.processmodule (acrord32.exe),
system.diagnostics.processmodule (ntdll.dll), syst
em.diagnostics.processmodule (kernel32.dll), syste
m.diagnostics.processmodule (kernelbase.dll)...}
nonpagedsystemmemorysize : 14584
nonpagedsystemmemorysize64 : 14584
pagedmemorysize64 : 63488000
pagedsystemmemorysize : 302460
pagedsystemmemorysize64 : 302460
peakpagedmemorysize : 75399168
peakpagedmemorysize64 : 75399168
peakworkingset : 87871488
peakworkingset64 : 87871488
peakvirtualmemorysize : 257703936
peakvirtualmemorysize64 : 257703936
priorityboostenabled : true
privatememorysize64 : 63488000
privilegedprocessortime : 00:00:27.7057776
processname : acrord32
processoraffinity : 3
responding : true
sessionid : 1
startinfo : system.diagnostics.processstartinfo
starttime : 2012/1/13 10:25:34
synchronizingobject :
threads : {4376, 6636, 8096, 5136...}
userprocessortime : 00:01:08.8276412
virtualmemorysize64 : 234819584
enableraisingevents : false
standardinput :
standardoutput :
standarderror :
workingset64 : 32616448
site :
container :
根据进程名过滤所有记事本进程。
ps c:powershell> get-process | where-object {$_.name -eq "notepad"}
handles npm(k) pm(k) ws(k) vm(m) cpu(s) id processname
------- ------ ----- ----- ----- ------ -- -----------
158 7 8800 37264 114 18.41 6204 notepad
根据进程名过滤所有ie进程。
ps c:powershell> get-process | where-object {$_.name -eq "iexplore"}
handles npm(k) pm(k) ws(k) vm(m) cpu(s) id processname
------- ------ ----- ----- ----- ------ -- -----------
710 23 12832 18160 175 10.51 4204 iexplore
971 39 81000 107580 399 22.20 6764 iexplore
336 13 28516 20096 187 0.34 6792 iexplore
929 35 51020 46568 314 10.42 7192 iexplore
835 26 49200 32360 308 7.82 7952 iexplore
根据company过滤所有产品发布者以”microsoft”打头的进程:
ps c:powershell> get-process | where-object {$_.company -like '*microsoft*' }|
select name,description,company
msseces microsoft security clie... microsoft corporation
notepad 记事本 microsoft corporation
onenotem microsoft onenote quick... microsoft corporation
outlook microsoft outlook microsoft corporation
powershell windows powershell microsoft corporation
prevhost preview handler surroga... microsoft corporation
rdcman rdcman microsoft corporation
searchprotocolhost microsoft windows searc... microsoft corporation
taskhost windows 任务的主机进程 microsoft corporation
使用别名
因为where-object的使用概率比较高,所以有一个很形象的别名 ? 可以使用:
ps c:powershell> get-service | ? {$_.name -like "b*"}
status name displayname
------ ---- -----------
running bdesvc bitlocker drive encryption service
running bfe base filtering engine
running bits background intelligent transfer ser...
stopped browser computer browser
stopped bthserv bluetooth support service