Sort-Object has an awesome feature: with the parameter -Unique, you can remove duplicates:
PS C:\> 1,3,2,3,2,1,2,3,4,7 | Sort-Object
1
1
2
2
2
3
3
3
4
7
PS C:\> 1,3,2,3,2,1,2,3,4,7 | Sort-Object -Unique
1
2
3
4
7
|
This can be applied to object results, as well. Check out this example: it will get you the latest 40 errors from your system event log:- Get-EventLog -LogName System -EntryType Error -Newest 40 | Sort-Object -Property InstanceID, Message | Out-GridView
复制代码 This may be perfectly fine, but depending on your event log, you may also get duplicate entries.
With -Unique, you can eliminate duplicates, based on multiple properties:- Get-EventLog -LogName System -EntryType Error -Newest 40 | Sort-Object -Property InstanceID, Message -Unique | Out-GridView
复制代码 You will no longer see more than one entry with the same InstanceID AND Message.
You can then sort the result again, to get back the chronological order:- Get-EventLog -LogName System -EntryType Error -Newest 40 | Sort-Object -Property InstanceID, Message -Unique | Sort-Object -Property TimeWritten -Descending | Out-GridView
复制代码 So bottom line is: Sort-Objects parameter -Unique can be applied to multiple properties at once.
http://powershell.com/cs/blogs/tips/archive/2014/03/04/eliminating-duplicates.aspx |