使用Group-Object(g*roup)按组统计*
PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | group Noun
Count Name Group
----- ---- -----
3 LocalGroupMember {Add-LocalGroupMember, Get-LocalGroupMember, Remove-LocalGroupMember}
7 LocalUser {Disable-LocalUser, Enable-LocalUser, Get-LocalUser, New-LocalUser, Remove-LocalUser, Rename-LocalUser, Set-LocalUser}
5 LocalGroup {Get-LocalGroup, New-LocalGroup, Remove-LocalGroup, Rename-LocalGroup, Set-LocalGroup}
对统计的结果进行排序,注意:以下两个语句是等效的
PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | sort Noun | group Noun
Count Name Group
----- ---- -----
5 LocalGroup {Get-LocalGroup, New-LocalGroup, Remove-LocalGroup, Rename-LocalGroup, Set-LocalGroup}
3 LocalGroupMember {Add-LocalGroupMember, Get-LocalGroupMember, Remove-LocalGroupMember}
7 LocalUser {Disable-LocalUser, Enable-LocalUser, Get-LocalUser, New-LocalUser, Remove-LocalUser, Rename-LocalUser, Set-LocalUser}
PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | group Noun | sort Name
Count Name Group
----- ---- -----
5 LocalGroup {Get-LocalGroup, New-LocalGroup, Remove-LocalGroup, Rename-LocalGroup, Set-LocalGroup}
3 LocalGroupMember {Add-LocalGroupMember, Get-LocalGroupMember, Remove-LocalGroupMember}
7 LocalUser {Disable-LocalUser, Enable-LocalUser, Get-LocalUser, New-LocalUser, Remove-LocalUser, Rename-LocalUser, Set-LocalUser}
**在Format-Table里使用-GroupBy按组显示结果,必须先对Groupby的字段进行排序,否则Groupby的字段不会显示在一起
**
PS C:\> **Get-Command -Module Microsoft.PowerShell.LocalAccounts | sort Noun | ft -GroupBy Noun
**
Noun: LocalGroup
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet New-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Remove-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Rename-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Set-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Noun: LocalGroupMember
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Add-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Get-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Remove-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Noun: LocalUser
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Disable-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Enable-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Get-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet New-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Remove-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Rename-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Set-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Format-Table处理以后,一般不能再进行"|"操作,否则会出错
PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | ft -AutoSize
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Add-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Disable-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Enable-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Get-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Get-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Get-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet New-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet New-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Remove-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Remove-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Remove-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Rename-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Rename-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Set-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Set-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | ft -AutoSize | sort Name
out-lineoutput : The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData" is not valid or not in the
correct sequence. This is likely caused by a user-specified "format-*" command which is conflicting with the default formatting
.
+ CategoryInfo : InvalidData: (:) [out-lineoutput], InvalidOperationException
+ FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand
有时候虽然能执行,但结果也和预期不符
PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | ft -AutoSize | group Noun
Count Name Group
----- ---- -----
19 {Microsoft.PowerShell.Commands.Internal.Format.FormatStartData, Microsoft.PowerShell.Command…
PS C:\> $data = Get-Command -Module Microsoft.PowerShell.LocalAccounts | ft -AutoSize | group Noun
PS C:\> $data.Group
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Add-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Disable-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Enable-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Get-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Get-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Get-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet New-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet New-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Remove-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Remove-LocalGroupMember 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Remove-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Rename-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Rename-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Set-LocalGroup 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Set-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
**最后,Group-Object(group)这个cmdlet在Microsoft.PowerShell.Utility这个Module里,不在Microsoft.PowerShell.Core里
**
PS C:\> Get-Command *-Object* | sort Source | ft -AutoSize
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet ForEach-Object 3.0.0.0 Microsoft.PowerShell.Core
Cmdlet Where-Object 3.0.0.0 Microsoft.PowerShell.Core
Cmdlet Compare-Object 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Group-Object 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Measure-Object 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet New-Object 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Register-ObjectEvent 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Select-Object 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Sort-Object 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Tee-Object 3.1.0.0 Microsoft.PowerShell.Utility
PS C:\> Alias | where DisplayName -like "*Object*" | select Definition, Name | sort Definition
Definition Name
---------- ----
Compare-Object compare
Compare-Object diff
ForEach-Object %
ForEach-Object foreach
Get-WmiObject gwmi
Group-Object group
Measure-Object measure
Remove-WMIObject rwmi
Select-Object select
Sort-Object sort
Tee-Object tee
Where-Object ?
Where-Object where
手机扫一扫
移动阅读更方便
你可能感兴趣的文章