Powershell xcopy for file backup

A handy powershell script for xcopying data – useful for database backups that need copying to tape:

function xCopy
(
[string]$src = $(throw "Specify the source directory"),
[string]$dest = $(throw "Specify the destination diirectory")
)
{
$src = $src -replace '\*$'
if (test-path $dest)
{
switch -regex ($src)
{
'\\$' {$src = "$src*"; break}
'\w$' {$src = "$src\*"; break}
default {break}
}
}
copy-item $src $dest -recurse -force
}


xCopy "C:\Source Folder\Location" "\\servername\foldershare\valid UNC path"

Save this to a script “xcopy.ps1”  and then execute as a scheduled task:

powershell “c:\xcopy.ps1” -arguments

Comments

4 responses to “Powershell xcopy for file backup”

  1. Steven Summone Avatar

    Thanks I tried this and it works a treat. I does not check if files are older than a certain date but it does not copy file across if they are already there and I can modify it myself.

    Cheers

    Steven

  2. Shadpantle Avatar

    New to powershell (and scripting). Would appreciate knowing about how to implement the date checking for this script.

    Thanks,

    Shad

  3. Verena Avatar
    Verena

    Add -verbose to the copy-file to get the information on the copy into the log.

    1. Mark Cooper Avatar

      Great tip – thanks Verena.

Leave a comment