xcopy files created this month in PowerShell

I wrote this code to copy files, what I need is a way to make it copy just the files created this month or created last 31 days?

$a = "L:\EndMonths\"
$a +=get-date -format MMMM
xcopy "L:\28*.zip" $a /I
xcopy "L:\29*.zip" $a /I
xcopy "L:\30*.zip" $a /I
xcopy "L:\31*.zip" $a /I

1 Answer

To get files from this month, use:

$date = Get-Date -Format M-1-y

or to get files from the last 31 days use:

$date = (Get-Date).AddDays(-31).ToString('M-d-y')

Then call xcopy like:

xcopy "L:\28*.zip" $a /I /D:$date
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like