Powershell to move files from folder 1 to folder 2 based on hours of creation

I am looking for a script that can be scheduled to run every 15 min to identify folders in a UNC like \\fileserver\oldfolder and move the files older than 2 hours to a new UNC location like \\fileserver\newfolder.

Please advise and provide a script to it.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Benjamin VoglarIT ProCommented:
How to scedule powershell script :


Save script below as xxx.ps1

Get-ChildItem -Path "\\share\folder" |
Where-Object {
  [datetime]::ParseExact($_.BaseName, "yyyyMMddHHmmss", $null) -gt (Get-Date).AddMinutes(-15)
} |
Move-Item -Destination "\\shre\folder2"

Open in new window

auk_expertsAuthor Commented:
Thanks for responding , i got below error when i changed and executed . May be its date format what we use ?

Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At C:\Users\administrator.AUK\Desktop\movefolder.ps1:3 char:25
+   [datetime]::ParseExact <<<< ($_.BaseName, "yyyyMMddHHmmss", $null) -gt (Get-Date).AddMinutes(-15)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Benjamin VoglarIT ProCommented:
I am sorry. I did not tested it.

Here is the correct code:

Get-ChildItem -Path "\\share" |
Where-Object {$_.CreationTime -gt (get-date).AddMinutes(-15)} |
Move-Item -Destination "\\share2"

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

auk_expertsAuthor Commented:
With above i do not get any errors but i don't see any action also as there are some old files in the folder but they are not moved or anything done.
Benjamin VoglarIT ProCommented:
Oh. I read the question aggain:

Change from -15 to -240   This is 2 h

And then change the -gt  (greater than) to -lt (less than)
auk_expertsAuthor Commented:
Thanks, without change the -gt it worked  great...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.