47

I use a Powershell command to generate a CSV report of certain file types. My goal is to find out how many were added during a particular date range. Right now, the script finds everything and I sort by date to find my number. I'd like to modify the command to only return objects within a creation date rage, i.e. if this file was created between 1 March 2013 and 31 March 2013. There's probably a way to limit the command by a date range, likely using Select-Object, I just can't figure it out.

Get-ChildItem 'PATH' -recurse -include @("*.tif*","*.jp2","*.pdf") | Select-Object FullName, CreationTime, @{Name="Mbytes";Expression={$_.Length/1Kb}}, @{Name="Age";Expression={(((Get-Date) - $_.CreationTime).Days)}} | Export-Csv 'PATH\scans.csv'
3
  • For the record, I'm not familiar with Powershell and did not know what to search for. The command I was using was grabbed from elsewhere online. I did search Google to find this answer and was not able to; that's where my thought about using Select-Object came from. I reject and resent your statements, this is supposed to be a help forum; but, I appreciate your help.
    – Nathan
    Commented Apr 8, 2013 at 18:52
  • 3
    +1 because Nathan did make an effort. There's a difference between someone who's unfamiliar with a technology (and may not know even what to search for) and someone making no effort. [Clarifying my earlier comment--"+1" indicates I upvoted your question to counteract the downvotes] :-) Commented Apr 8, 2013 at 19:11
  • 1
    I guess I just expect more. A simple search for "powershell get files creation date" on google would give him everything except -and on the first hit, and you don't need powershell knowledge to come up with that search phrase =)
    – Frode F.
    Commented Apr 9, 2013 at 16:26

3 Answers 3

71

Use Where-Object and test the $_.CreationTime:

Get-ChildItem 'PATH' -recurse -include @("*.tif*","*.jp2","*.pdf") | 
    Where-Object { $_.CreationTime -ge "03/01/2013" -and $_.CreationTime -le "03/31/2013" }
3

Use Where-Object, like:

Get-ChildItem 'PATH' -recurse -include @("*.tif*","*.jp2","*.pdf") | 
Where-Object { $_.CreationTime -gt "03/01/2013" -and $_.CreationTime -lt "03/31/2013" }
Select-Object FullName, CreationTime, @{Name="Mbytes";Expression={$_.Length/1Kb}}, @{Name="Age";Expression={(((Get-Date) - $_.CreationTime).Days)}} | 
Export-Csv 'PATH\scans.csv'
2

Fixed it...

Get-ChildItem C:\Windows\ -recurse -include @("*.txt*","*.pdf") |
Where-Object {$_.CreationTime -gt "01/01/2013" -and $_.CreationTime -lt "12/02/2014"} | 
Select-Object FullName, CreationTime, @{Name="Mbytes";Expression={$_.Length/1Kb}}, @{Name="Age";Expression={(((Get-Date) - $_.CreationTime).Days)}} | 
Export-Csv C:\search_TXT-and-PDF_files_01012013-to-12022014_sort.txt
1
  • please use block quote by appending 4 spaces to Get-ChildItem
    – HKTonyLee
    Commented Apr 28, 2015 at 4:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.