Powershell – how to use powershell to get childitem of the parent folder and pick up the latest update child folder

network-sharepowershell

$var.droppath.TrimEnd() # is the parent folder

all child folder were named with "10.**"…

I can use the following line to get all child folders:

get-childitem $var.droppath.TrimEnd()|where-object {$_.name -match "10."} 

here is a list of one of the childitem I got:

 PSPath            : Microsoft.PowerShell.Core\FileSystem::\\sharespace\test1\10.00.1220.00
 PSParentPath      : Microsoft.PowerShell.Core\FileSystem::\\sharespace\test1\     PSChildName       : 10.00.1220.00
 PSProvider        : Microsoft.PowerShell.Core\FileSystem
 PSIsContainer     : True
 Name              : 10.00.1220.00
 Parent            : CU
 Exists            : True
 Root              : \\sharespace
 FullName          : \\sharespace\test1\10.00.1220.00
 Extension         : .00
 CreationTime      : 1/8/2011 3:24:01 PM
 CreationTimeUtc   : 1/8/2011 7:24:01 AM
 LastAccessTime    : 6/25/2011 2:51:33 AM
 LastAccessTimeUtc : 6/24/2011 6:51:33 PM
 LastWriteTime     : 6/25/2011 2:51:33 AM
 LastWriteTimeUtc  : 6/24/2011 6:51:33 PM
 Attributes        : Directory
 BaseName          : 10.00.4272.00
 Mode              : d----

Can I filter the latest updated child folder form above?

Best Answer

This example assumes that by "latest update child folder" you want to use the LastWriteTime attribute.

Get-ChildItem "10.*" | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1