Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find latest modified file information in PowerShell

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?

like image 380
The Woo Avatar asked Jun 04 '14 02:06

The Woo


People also ask

How do I get the date modified in PowerShell?

To get all the files that are modified after certain days, we need to use the LastWriteTime property. The below command shows us the files which are modified within the last 30 days in the C:\temp folder. You can also use the AddMonths() or AddYears() instead of AddDays() as per your requirement.

How do I get LastWriteTime in PowerShell?

Use the Get-ChildItem to get files where lastwritetime is today. It will check lastwritetime is greater than yesterday's date. In the above PowerShell script, the Get-ChildItem cmdlet search for the files within the path specified recursively and check if the lastwritetime is today.

How do I find a ChildItem file by date and time?

Finding Old Files In this case, we're going to look at the LastWriteTime for each file. In this example, I want to show all files older than 30 days. In order to do that, we have to get the current date with Get-Date, subtract 30 days and then grab everything less than (older than) the resulting date.


2 Answers

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
like image 110
JPBlanc Avatar answered Nov 13 '22 13:11

JPBlanc


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.

like image 25
user3704400 Avatar answered Nov 13 '22 14:11

user3704400