Question

Im new to powershell - so serious noob.

But I wanted to see if anyone could help in doing the following.

We have a folder on a server that has reports written to it every night. The reports are named in the following format: DiskSpaceReport_26102012.html and location of C:\Powershell\WebReport\

I would like a PS script to copy these 1 of these files from the folder using a daterange of -8 Days from the date the script runs - the script would be run as part of a windows scheduled task or through SQL Agent job.

So at present there are 8 files in the folder dating from Friday 26 Oct to Friday 19th Oct. I would like the process to run today and copy the file -8 days back from todays date. So copy the file named DiskSpaceReport_19102012.html

And this process should repeat weekly on friday and copy the last file from 8 days ago. The copy is to a network share

\\Server01\Powershell\Webreports_Archive

And as I mentioned in title I dont mind if this is easier to do via robocopy in a batch file for example. Would prefer it via PS though.

Was it helpful?

Solution

The following will do what you want:

$pastdays = -8
$pastdate = [datetime]::Now.AddDays($pastdays)
$filename = "DiskSpaceReport_" + $pastdate.Day + $pastdate.Month + $pastdate.Year+".html"
Copy-Item -Path "C:\Powershell\WebReport\$($filename)" "\\Server01\Powershell\Webreports_Archive"

regards Jon

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top