Find latest modified file information in PowerShell

The Woo picture The Woo · Jun 4, 2014 · Viewed 33.5k times · Source

I run this code:

Get-ChildItem 'C:\Test Folder' | Where {$_.LastWriteTime} | select -last 1

And I get back Mode, LastWriteTime, Length, and Name of the last modified file - great!

I'm trying to get the username of the file's owner as well.

I've added this code:

| ForEach-Object {Get-Acl $_.FullName}

Which returns Path, Owner, Access for the file.

How can I display LastWriteTime, and Owner to be the only objects shown in the output?

Answer

JPBlanc picture JPBlanc · Jun 4, 2014

Are you sure that what you are trying to do is not the following?

Get-ChildItem 'C:\Test Folder' | Sort {$_.LastWriteTime} | select -last 1

You can try this:

$c = Get-ChildItem 'C:\Test Folder' | Sort {$_.LastWriteTime} | select -last 1 | foreach {$a=$_;$b=Get-Acl $_.FullName; Add-Member -InputObject $b -Name "LastWriteTime" -MemberType NoteProperty -Value $a.LastWriteTime;$b}
$c.LastWriteTime