get all files created between x hours and y hours

Hi

The below will show me all files created in last 24 hours

get-childitem -recurse | where-object { $_.lastwritetime -gt (get-date).addHours(-24)}

Can someone advise how to modify it so I can check between x hours and y hours - e.g. so I can check for all files created between 3 hours ago and 6 hours ago - e.g. created in a specific time period

Of course.

But before that, I would suggest you to take little more time and understand how it works for last 24 hrs(see where number 24 is used there) and try changing that number and see the effects.

yes I know I can change that number and get all in last 12 hours or 6 hours - and clearly know what effects it has on results

BUT I want to enhance it as per question so I can get between x hours and y hours, and changing that one number doesn’t help me, hence asking question!

 

It’s actually not that hard. Instead of one condition you need two and you have to combine them … did you try to google that beginner question? :wink: :smiley:

Get-ChildItem -Recurse | Where-Object { $.LastWriteTime -gt (Get-Date).AddHours(-12) -and $.LastWriteTime -gt (Get-Date).AddHours(-6) }

  1. Yes I did google first
  2. Obviously not that simple as solution you gave doesn't work as both conditions use -gt and even I worked out one needs to be -lt
Results using the "actually not that hard" reply from Olaf in a folder containing 5 files, four of them created 2 1/2 hours ago and one created 2 minutes ago - if I use this
 Get-ChildItem -Recurse | Where-Object { $_.LastWriteTime -gt (Get-Date).AddHours(-5) -and $_.LastWriteTime -gt (Get-Date).AddHours(-1) }
Then it only shows me the file created 2 minutes ago - NOT the files created between 1 hour and 5 hours ago

Changing it to use -lt as below

 Get-ChildItem -Recurse | Where-Object { $_.LastWriteTime -gt (Get-Date).AddHours(-5) -and $_.LastWriteTime -lt (Get-Date).AddHours(-1) }

correctly gives me the four files created 2 1/2 hours ago

Thank you both for your replies to this “not so hard” question, I had googled and not got a result, and thought I would ask on this “friendly” forum - in return I get sarcasm and a wrong answer, but at least it guided me to the right answer :slight_smile:

[quote quote=155274]

  1. Yes I did google first
  2. [/quote] Great. So you're obviously a step ahaed a lot of others. [quote quote=155274]
  3. Obviously not that simple as solution you gave doesn't work as both conditions use -gt and even I worked out one needs to be -lt
[/quote] You're right. I've made a mistake. I didn't test before I posted. Sorry about that. Nobody is perfect. [quote quote=155274] correctly gives me the four files created 2 1/2 hours ago[/quote] Great. I'm glad you finaly achieved what you was looking for. [quote quote=155274] but at least it guided me to the right answer [/quote] Isn't that great? We both learned something today. Have a nice Weekend.

Thanks for your help :slight_smile: much appreciated

Great that you’ve found the solution and glad that you fixed it yourself. We are always glad when the requestor finds the answer of their own. Instead of jumping to the answer we try to guide by giving hints. Hence He/She learns How it works and Why is it so.