使用Group-Object(group)按组统计
1 PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | group Noun 2 3 Count Name Group 4 ----- ---- ----- 5 3 LocalGroupMember {Add-LocalGroupMember, Get-LocalGroupMember, Remove-LocalGroupMember} 6 7 LocalUser {Disable-LocalUser, Enable-LocalUser, Get-LocalUser, New-LocalUser, Remove-LocalUser, Rename-LocalUser, Set-LocalUser} 7 5 LocalGroup {Get-LocalGroup, New-LocalGroup, Remove-LocalGroup, Rename-LocalGroup, Set-LocalGroup}
对统计的结果进行排序,注意:以下两个语句是等效的
1 PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | sort Noun | group Noun 2 3 Count Name Group 4 ----- ---- ----- 5 5 LocalGroup {Get-LocalGroup, New-LocalGroup, Remove-LocalGroup, Rename-LocalGroup, Set-LocalGroup} 6 3 LocalGroupMember {Add-LocalGroupMember, Get-LocalGroupMember, Remove-LocalGroupMember} 7 7 LocalUser {Disable-LocalUser, Enable-LocalUser, Get-LocalUser, New-LocalUser, Remove-LocalUser, Rename-LocalUser, Set-LocalUser} 8 9 10 11 PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | group Noun | sort Name 12 13 Count Name Group 14 ----- ---- ----- 15 5 LocalGroup {Get-LocalGroup, New-LocalGroup, Remove-LocalGroup, Rename-LocalGroup, Set-LocalGroup} 16 3 LocalGroupMember {Add-LocalGroupMember, Get-LocalGroupMember, Remove-LocalGroupMember} 17 7 LocalUser {Disable-LocalUser, Enable-LocalUser, Get-LocalUser, New-LocalUser, Remove-LocalUser, Rename-LocalUser, Set-LocalUser}
在Format-Table里使用-GroupBy按组显示结果,必须先对Groupby的字段进行排序,否则Groupby的字段不会显示在一起
1 PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | sort Noun | ft -GroupBy Noun
2
3
4 Noun: LocalGroup
5
6 CommandType Name Version Source
7 ----------- ---- ------- ------
8 Cmdlet Get-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
9 Cmdlet New-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
10 Cmdlet Remove-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
11 Cmdlet Rename-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
12 Cmdlet Set-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
13
14
15 Noun: LocalGroupMember
16
17 CommandType Name Version Source
18 ----------- ---- ------- ------
19 Cmdlet Add-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
20 Cmdlet Get-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
21 Cmdlet Remove-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
22
23
24 Noun: LocalUser
25
26 CommandType Name Version Source
27 ----------- ---- ------- ------
28 Cmdlet Disable-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
29 Cmdlet Enable-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
30 Cmdlet Get-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
31 Cmdlet New-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
32 Cmdlet Remove-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
33 Cmdlet Rename-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
34 Cmdlet Set-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Format-Table处理以后,一般不能再进行"|"操作,否则会出错
1 PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | ft -AutoSize
2
3 CommandType Name Version Source
4 ----------- ---- ------- ------
5 Cmdlet Add-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
6 Cmdlet Disable-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
7 Cmdlet Enable-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
8 Cmdlet Get-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
9 Cmdlet Get-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
10 Cmdlet Get-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
11 Cmdlet New-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
12 Cmdlet New-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
13 Cmdlet Remove-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
14 Cmdlet Remove-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
15 Cmdlet Remove-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
16 Cmdlet Rename-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
17 Cmdlet Rename-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
18 Cmdlet Set-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
19 Cmdlet Set-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
20
21
22
23 PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | ft -AutoSize | sort Name
24
25 out-lineoutput : The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData" is not valid or not in the
26 correct sequence. This is likely caused by a user-specified "format-*" command which is conflicting with the default formatting
27 .
28 + CategoryInfo : InvalidData: (:) [out-lineoutput], InvalidOperationException
29 + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand
有时候虽然能执行,但结果也和预期不符
1 PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | ft -AutoSize | group Noun
2
3 Count Name Group
4 ----- ---- -----
5 19 {Microsoft.PowerShell.Commands.Internal.Format.FormatStartData, Microsoft.PowerShell.Command...
6
7
8
9 PS C:\> $data = Get-Command -Module Microsoft.PowerShell.LocalAccounts | ft -AutoSize | group Noun
10
11 PS C:\> $data.Group
12
13 CommandType Name Version Source
14 ----------- ---- ------- ------
15 Cmdlet Add-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
16 Cmdlet Disable-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
17 Cmdlet Enable-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
18 Cmdlet Get-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
19 Cmdlet Get-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
20 Cmdlet Get-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
21 Cmdlet New-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
22 Cmdlet New-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
23 Cmdlet Remove-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
24 Cmdlet Remove-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
25 Cmdlet Remove-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
26 Cmdlet Rename-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
27 Cmdlet Rename-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
28 Cmdlet Set-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
29 Cmdlet Set-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
最后,Group-Object(group)这个cmdlet在Microsoft.PowerShell.Utility这个Module里,不在Microsoft.PowerShell.Core里
1 PS C:\> Get-Command *-Object* | sort Source | ft -AutoSize
2
3 CommandType Name Version Source
4 ----------- ---- ------- ------
5 Cmdlet ForEach-Object 3.0.0.0 Microsoft.PowerShell.Core
6 Cmdlet Where-Object 3.0.0.0 Microsoft.PowerShell.Core
7 Cmdlet Compare-Object 3.1.0.0 Microsoft.PowerShell.Utility
8 Cmdlet Group-Object 3.1.0.0 Microsoft.PowerShell.Utility
9 Cmdlet Measure-Object 3.1.0.0 Microsoft.PowerShell.Utility
10 Cmdlet New-Object 3.1.0.0 Microsoft.PowerShell.Utility
11 Cmdlet Register-ObjectEvent 3.1.0.0 Microsoft.PowerShell.Utility
12 Cmdlet Select-Object 3.1.0.0 Microsoft.PowerShell.Utility
13 Cmdlet Sort-Object 3.1.0.0 Microsoft.PowerShell.Utility
14 Cmdlet Tee-Object 3.1.0.0 Microsoft.PowerShell.Utility
15
16
17
18 PS C:\> Alias | where DisplayName -like "*Object*" | select Definition, Name | sort Definition
19
20 Definition Name
21 ---------- ----
22 Compare-Object compare
23 Compare-Object diff
24 ForEach-Object %
25 ForEach-Object foreach
26 Get-WmiObject gwmi
27 Group-Object group
28 Measure-Object measure
29 Remove-WMIObject rwmi
30 Select-Object select
31 Sort-Object sort
32 Tee-Object tee
33 Where-Object ?
34 Where-Object where