Copy files not older than 15 min using PowerShell Script

 

Requirement

There is requirement of copying files as old as 15 min to an destination folder. Older than 15 min files should be retained at the source location.

Solution

Below is the simple PowerShell based code to achieve the above requirement.

Fetch all files from a specific folder with metadata

Get-ChildItem “C:\Amjad\SourceFiles\” -File | select name, *time

PS1

Fetch files only as old as 15 minutes

Get-ChildItem  -Path “C:\Amjad\SourceFiles\*” | where {$_.LastWriteTime -ge (Get-Date).AddMinutes(-15)} | select name, *time

PS2

 

Copy the files only as old as 15 minutes to Destination folder

Get-ChildItem  -Path “C:\Amjad\SourceFiles\*” | where {$_.LastWriteTime -ge (Get-Date).AddMinutes(-15)} | Copy-Item -Destination C:\Amjad\ArchivalDestination

 

Similarly if you want to move all files by Creation time to be copied to destination folder,

Get-ChildItem  -Path “C:\Amjad\SourceFiles\*” | where {$_.CreationTime -ge (Get-Date).AddMinutes(-15)} | Copy-Item -Destination C:\Amjad\ArchivalDestination

 

Similarly if you want to move all files by Last Access time to be copied to destination folder,

Get-ChildItem  -Path “C:\Amjad\SourceFiles\*” | where {$_.LastAccessTime -ge (Get-Date).AddMinutes(-15)} | Copy-Item -Destination C:\Amjad\ArchivalDestination

Hope this article would be useful.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s