Group-Object is magic !

Group-Object is magic !

I had to create a script that sort a whole csv, each line is an extract of a computer scan, one extract per day, and get only the last extract per computer AND per user.

Indeed, a user can use multiple computers, and several users can use the same computer, the extract result is not the same !

Here is the line that “saved” me :

Import-Csv $file -delimiter ";" | 
Group-Object 'Username','Computer' |
Foreach-Object{ $_.Group | Sort "Last Update" -Descending | Select -First 1} |
Sort 'Username','Computer' 
Export-Csv $DestFile -NoTypeInformation

That line while sort that perfectly, and export to the destination csv all fields.

It’s like sorting M&M’s regarding their expiration date !

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site