none
How to use PowerShell to find files created in the last 15 min and bigger than 5 KB RRS feed

  • Question

  • Hi! 

    I am trying to write to a text file, the files created in the last 15 min and bigger than 5KB

    I have created this PowerShell script but it's not working. Can somebody help me with this?

    $Notprocessedfiles = Get-ChildItem c:\test | where {$_.Lastwritetime -gt (date).addminutes(-15)}
    if ($Notprocessedfiles.lenght -gt 3KB) 
    {
    $Notprocessedfiles | Out-file c:\test\maybe.txt
    }

    Thanks!

    Monday, March 31, 2014 4:02 PM

Answers

  • Do it the PowerShell way.  It is easier and faster.

    Get-ChildItem c:\test |
          Where-Object{$_.Creationtime -gt (date).addMinutes(-15) -AND $_.Length -gt 5Kb} |
          Format-Table -Auto |
          Out-String |
          Out-file c:\test\maybe.txt


    ¯\_(ツ)_/¯



    • Edited by jrv Monday, March 31, 2014 5:36 PM
    • Marked as answer by Bill_StewartModerator Wednesday, July 9, 2014 2:17 PM
    Monday, March 31, 2014 5:35 PM

All replies

  • You've misspelled length in your If conditional.  I also noticed that your If conditional says 3KB when you said you are after files that are greater than 5KB. On a personal note, you might consider using the CreationTime property instead of LastWriteTime.

    Hope this helps - the rest seems to work.

    Edit: Added mismatch info between 5KB and 3KB.

    Monday, March 31, 2014 4:25 PM
  • Thanks for replying Tommy.

    I made the changes you recommended but I still don't get any output.

    $Notprocessedfiles = Get-ChildItem c:\test | where {$_.CreationTime -gt (date).addminutes(-15)}
    if ($Notprocessedfiles.length -gt 5KB)
     {
     $Notprocessedfiles | Out-file c:\test\maybe.txt
     }                     

    Monday, March 31, 2014 5:24 PM
  • Do it the PowerShell way.  It is easier and faster.

    Get-ChildItem c:\test |
          Where-Object{$_.Creationtime -gt (date).addMinutes(-15) -AND $_.Length -gt 5Kb} |
          Format-Table -Auto |
          Out-String |
          Out-file c:\test\maybe.txt


    ¯\_(ツ)_/¯



    • Edited by jrv Monday, March 31, 2014 5:36 PM
    • Marked as answer by Bill_StewartModerator Wednesday, July 9, 2014 2:17 PM
    Monday, March 31, 2014 5:35 PM
  • It's working perfectly.

    Thanks for your help guys!

    Monday, March 31, 2014 5:58 PM