Need sorting in Output ForEach

Hello,

I have a script for enumerating the folder-date of Progress software on my computers.

The script runs fine but I want to sort the output on the lastwrite time. I’m a little stuck here.
Can anyone please help me in the right direction?

The output I get is:

Root         LastWriteTime      
----         -------------      
\\WKS2000\c$ 1-12-2016 11:01:55 
\\WKS2001\c$ 1-12-2016 11:05:36 
\\WKS2002\c$ 1-12-2016 11:08:33 
\\WKS2005\c$ 6-6-2016 15:49:44  
\\WKS2007\c$ 6-6-2016 15:49:44  
\\WKS2008\c$ 1-12-2016 12:38:55 
\\WKS2009\c$ 31-10-2016 14:13:07
\\WKS2010\c$ 2-12-2016 14:12:21 
\\WKS2011\c$ 31-10-2016 14:13:06
\\WKS2012\c$ 31-10-2016 14:16:19
\\WKS2013\c$ 31-10-2016 14:13:05
\\WKS2015\c$ 31-10-2016 14:16:26
\\WKS2016\c$ 31-10-2016 14:13:21
\\WKS2017\c$ 31-10-2016 13:27:46
\\WKS2019\c$ 6-6-2016 15:49:44  
\\WKS2020\c$ 2-12-2016 11:19:36 
\\WKS2021\c$ 2-12-2016 14:52:01 
\\WKS2022\c$ 2-12-2016 09:20:00 
WKS2023 Software installation not found
\\WKS2024\c$ 31-10-2016 14:19:22
\\WKS2025\c$ 1-12-2016 12:59:26 
\\WKS2027\c$ 31-10-2016 14:13:04
\\WKS2028\c$ 2-12-2016 14:04:00 

The code I use is:

$allepcs2 = Get-ADComputer -Filter {(Name -like "wks*") -and (enabled -eq "true")}  | Where { Test-Connection $_.name -count 1 -quiet } |Select-Object -expand Name

foreach ($pc in $allepcs2) 
    {
    if ( Test-Path "\\$pc\c$\Program Files (x86)\Progress Software Corporation\OpenEdge 11.5 Shared Network Installation\bin" )
        {
        get-childitem "\\$pc\c$\Program Files (x86)\Progress Software Corporation\OpenEdge 11.5 Shared Network Installation\b*" |select root,lastwritetime
        }
    else
        {
        Write-host "$pc Software installation not found"
    }
 
}

Albert,

first - I would remove the ‘Write-Host’ line. If Don sees this he will not be amused. :wink:

Then you can store the result of your foreach loop in a variable and sort it then afterwards like this:

$Variable | Sort-Object -Property lastwritetime

Haha, good point. I will change it into a warning. (sorry, Don. I should know better as I’m reading your monthly lunches books)

on-topic: I was trying to do so as you suggested but I’m unable to put my foreach into a variable. Probably some basic thing but I can’t seem to get it.

How do I do this?

Hi Albert, should be as simple as below

$allepcs2 = Get-ADComputer -Filter {(Name -like "wks*") -and (enabled -eq "true")}  | Where { Test-Connection $_.name -count 1 -quiet } |Select-Object -expand Name

$FEVarible = foreach ($pc in $allepcs2) 
    {
    if ( Test-Path "\\$pc\c$\Program Files (x86)\Progress Software Corporation\OpenEdge 11.5 Shared Network Installation\bin" )
        {
        get-childitem "\\$pc\c$\Program Files (x86)\Progress Software Corporation\OpenEdge 11.5 Shared Network Installation\b*" |select root,lastwritetime
        }
    else
        {
#Every Time you use write-host god shoots a puppy#

        Write-host "$pc Software installation not found"
    }
 
}

$FEVarible | Sort-Object -Property lastwritetime

Change write-host so it creates a matching object.

new-object psobject -Property @{root=$pc;lastwritetime="Software installation not found"}

The sort will group them together.

Great, thanks… it was that simple :slight_smile:

And so should it be… :slight_smile: thanks for everything, I’m up and running now.