# Fastest Windows script to move files from one did to a network dir

I wrote a script that moves files older than two days over to a network share (via scheduled task). Windows Task Scheduler doesn't seem to like the script so much, but it works well by itself.

1. Is there a faster way than what I have?

2. Is there a way to programmatically make it a scheduled task or service of some type?

Backup.ps1

$today = get-date$tda= $today.addDays(-2) #two days ago$files = get-childitem d:\source_path | select fullname, lastwritetime | where-object{$_.lastwritetime -lt$tda}

foreach ($file in$files) {
Echo "moving " $file.fullname #just so I know it's still running, get rid of this? mv$file.fullname 'n:\dest_folder'
Echo 'done'
}

• What happens if the destination folder already has a file with that name ? – konijn Apr 22 '14 at 13:51
• It shouldn't, BUT the whole thing dies. – Jeff Apr 22 '14 at 14:15
• I would consider using  -force then -> technet.microsoft.com/en-us/library/ee176910.aspx – konijn Apr 22 '14 at 14:30

1. If there is a lot of files and you should look into using runspaces to run several copy-jobs in separate threads. Just be careful, you are still limited by the bandwidth of the network.

2. We need to see some kind of error to be certain, but I would start by using PowerShell commands instead of old dos commands.

Which leads me to the second part of this answer. The actual code review :)

First thing; if you are going to write a script in PowerShell, use PowerShell commands :) Replace echo with Write-Host and replace mv with Copy-Item.

Second: you don't need | select fullname, lastwritetime. It probably just adds (a little bit of) latency to the execution of the script.

As others have pointed out, you should add some error handling and thinking about what would happen if the destination folder already have a file with the same name. Using -Force is one way, but would overwrite the existing file. So if you don't want this, you will have to handle it differently.

Lastly, if you are new to PowerShell, start commenting your code. You'll thank yourself when you go back to review your scripts 6 months from now :)

Oh, by the way, If you read up on Copy-Item, there is a -Filter parameter that might make the whole Get-ChildItem part of the script redundant.