Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell Copy--Item special folders like My Documents

$
0
0

Hi, I'm using PowerShell 2.0 on Windows 7 and I'm just trying to write a simple script that will backup certain files/folders to an external USB drive.  The script reads a .csv file for the locations to backup and does a recursive backup of those locations using Copy-Item.  Where I run into problems is special folders like 'My Documents'.  If I specify 'Documents' instead of 'My Documents', then it fails when it hits 'My Music' or 'My Videos' within 'My Documents'.  There are other folders under 'My Documents' that aren't special folders that I also want it to backup hence the -recursive.  It seems like a pretty simple thing that someone might want to copy all of their 'My Documents' or do something like Get-ChildItem on them.  Is there a way to deal with Special Folders without having to write a line in my script to deal with every special folder?  ie.  I just want to backup locations like c:\myname, c:\user\myprofile\my Documents, c:\users\myprofile\appdata\roaming\<a particular apps settings>....

Here's the section of the script that reads from the .csv and creates a folder by the same name in the destination location.

This is the line that's doing the actual work:

Copy-Item -Path $SOURCE -Destination $BACKUPLOC2 -Recurse

Portion of script doing copy:

foreach ($LINE in $LIST){
    $SOURCE = $LINE.BackupSource
    $DEST = $SOURCE.split("\")[-1]
    $BACKUPLOC2 = $BACKUPLOCN + $DEST
    # copy files from source to destination
    LogWrite "Copy from $SOURCE to $BACKUPLOC2"
    Try
     {
     Copy-Item -Path $SOURCE -Destination $BACKUPLOC2 -Recurse
     LogWrite "====="
     }
    Catch
     {
     LogWrite "error copying files -  $_"
     Write-Host "error copying files = $_"
     LogWrite "====="
     }
 }


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>