Ok I have this script where I read in a CSV (converted from a XLS) and save an ID-field as well as a date-field into an array. Like so:
New-Object PSObject -prop @{
TheId = $_."The ID";
TheDate = [DateTime]::Parse($_."The Date")
}
It works like a charm and I get a nice array of objects with the two properties above. Now, I would like to get the count of items on each day, without having to do a foreach to loop through and such. Is there a nicer solution to it? (Note that TheDate contains different times too, which I'd like to ignore.)
Group-Object allows to use script blocks as -Property values. So that you can use Date property for grouping:
# demo: group temporary files by modification date (day!):
Get-ChildItem $env:temp | Group-Object {$_.LastWriteTime.Date}
# answer for your example:
$objects | Group-Object {$_.TheDate.Date}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With