Find latest modified file information in PowerShell
Asked Answered
U

2

9

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?

Undemonstrative answered 4/6, 2014 at 2:40 Comment(0)
G
16

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
Gobbet answered 4/6, 2014 at 7:38 Comment(0)
M
3

So the select will allow you to just get the properties you are interested in.

So a few things to do:

  1. Figure out what properties you could select from
    Get-ChildItem | Get-Member -membertype properties
    
  2. Once you know the properties just add to the select in your original statement

    Get-ChildItem'c:\test folder' | where {$_.lastwritetime} | select -last 1 | `
    foreach { write-host $_.lastwritetime ((get-ACL).owner)}
    

Finally, don't be afraid of the Get-Help command.

Manana answered 4/6, 2014 at 4:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.