Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get latest dates from an array per object group in Powershell

I have an array $data :

PS> $data
Datum                  User                   Computer                                      
-----                  ---------              --------                                      
10/06/2013 13:10:56    [email protected]    DC1$                                          
10/06/2013 13:09:25    [email protected]    DC2$                                          
10/06/2013 10:05:13    [email protected]    DC2$                                          
7/06/2013 16:32:47     [email protected]    DC1$    

I want to get the latest dates from the $data array for each computer like this:

PS> $result
Datum                  User                   Computer                                      
-----                  ---------              --------                                      
10/06/2013 13:10:56    [email protected]    DC1$                                          
10/06/2013 13:09:25    [email protected]    DC2$                                          

I really couldn't get this result. Could you please help me on this?

like image 645
Korki Korkig Avatar asked Jun 10 '13 13:06

Korki Korkig


People also ask

How do I get the latest date in PowerShell?

The Get-Date cmdlet gets a DateTime object that represents the current date or a date that you specify. Get-Date can format the date and time in several . NET and UNIX formats. You can use Get-Date to generate a date or time character string, and then send the string to other cmdlets or programs.

What is @() in PowerShell?

What is @() in PowerShell Script? In PowerShell, the array subexpression operator “@()” is used to create an array. To do that, the array sub-expression operator takes the statements within the parentheses and produces the array of objects depending upon the statements specified in it.

How do you access an array in PowerShell?

To access items in a multidimensional array, separate the indexes using a comma ( , ) within a single set of brackets ( [] ). The output shows that $c is a 1-dimensional array containing the items from $a and $b in row-major order.

How do I make a list of strings in PowerShell?

Create an Array of Strings Using the [String[]] Method in PowerShell. To work with an array of strings in PowerShell, firstly, we have to create them. By using the “[String[]]” method, we will create a “$var” array of strings. This “$var” array of strings will contain the values: “PowerShell”, “String”, and “Array”.


1 Answers

$data | Foreach-Object {$_.Datum = [DateTime]$_.Datum; $_} | 
Group-Object Computer | 
Foreach-Object {$_.Group | Sort-Object Datum | Select-Object -Last 1}
like image 59
Shay Levy Avatar answered Oct 23 '22 08:10

Shay Levy