none
Get modified dates for a list of files RRS feed

  • Question

  • I am trying to come up with a way to use a list of files for the input and gather a list of modified dates for those files.  I'd like it to take up a single line but if that's not realistic, then so be it.  I thought I had a solution to the problem but it doesn't work like I think it should and I'm not sure why.  I came up with two potential solutions but neither one work.

    • Get-Content c:\Users\me\Desktop\listoffilesandpath.txt | Foreach-Object {Write-Host $_.LastWriteTime}
    • Get-Content c:\Users\me\Desktop\listoffilesandpath.txt | Foreach-Object {Get-ChildItem $_.LastWriteTime}

    The first doesn't display anything at all and the second gets the write times for everything in the directory that I'm in - neither seems to use the non-empty list located at c:\Users\me\Desktop\listoffilesandpath.txt.  Clearly there is something that I don't understand about how these commands interact with one another.  What am I doing wrong here?

    Friday, June 10, 2016 3:53 AM

Answers

  • What is the purpose? Get-ChildItem returns file objects. If you want to know the last write times of files, just ask for it. For example:


    PS C:\> Get-ChildItem C:\Windows\*.exe |
      Select-Object FullName,LastWriteTime
    

    Remember that LastWriteTime is a DateTime object, so you can sort, compare, etc.

    It may be helpful to you to get a basic overview of how PowerShell is object-oriented. Start with this:

    Windows IT Pro - PowerShell: Objects and Output


    -- Bill Stewart [Bill_Stewart]

    Friday, June 10, 2016 4:19 AM
    Moderator

  • Get-Content "ListOfFiles.txt" | ForEach-Object {
      Get-Item $_ | Select-Object FullName,LastWriteTime
    }
    

    You can pipe to Export-Csv to create a CSV file.


    -- Bill Stewart [Bill_Stewart]

    Friday, June 10, 2016 2:54 PM
    Moderator

All replies

  • What is the purpose? Get-ChildItem returns file objects. If you want to know the last write times of files, just ask for it. For example:


    PS C:\> Get-ChildItem C:\Windows\*.exe |
      Select-Object FullName,LastWriteTime
    

    Remember that LastWriteTime is a DateTime object, so you can sort, compare, etc.

    It may be helpful to you to get a basic overview of how PowerShell is object-oriented. Start with this:

    Windows IT Pro - PowerShell: Objects and Output


    -- Bill Stewart [Bill_Stewart]

    Friday, June 10, 2016 4:19 AM
    Moderator
  • Thanks for the reply, Bill - much appreciated.

    The purpose is that I have a list of files (including the full path) in a text file and I need to get the dates that they were modified.  It's a long list and I don't want to gather those manually for each entry.  How do I pass the contents of that text file into the example that you gave?

    Friday, June 10, 2016 2:42 PM

  • Get-Content "ListOfFiles.txt" | ForEach-Object {
      Get-Item $_ | Select-Object FullName,LastWriteTime
    }
    

    You can pipe to Export-Csv to create a CSV file.


    -- Bill Stewart [Bill_Stewart]

    Friday, June 10, 2016 2:54 PM
    Moderator
  • Awesome - thanks, Bill!
    Friday, June 10, 2016 3:46 PM