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

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!

March 31st, 2014 12:07pm

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.

Free Windows Admin Tool Kit Click here and download it now
March 31st, 2014 12:30pm

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
 }                     

March 31st, 2014 1:29pm

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

Free Windows Admin Tool Kit Click here and download it now
March 31st, 2014 1:40pm

It's working perfectly.

Thanks for your help guys!

March 31st, 2014 2:03pm

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.

Free Windows Admin Tool Kit Click here and download it now
March 31st, 2014 7:25pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics