Copy certain folders (and subfolders) only.

by Adzzz at 2012-09-26 03:28:43

Hi all, I’m Adam… I’m new to the site and new to PowerShell scripting and this site looks pretty useful.

My experience with PS scripts is… well, I’ve written a couple of scripts to copy a few files and send emails following the copy, thanks to a lot of Googling, and have set myself a challenge to get another script written. My background is excel and access, VBA, and SQL and web stuff, although I wrote batch files many moons ago back in the old Dos 5.0 and 6.22 days, but am pretty rusty. I’d consider myself a beginner at powershell, but can see its potential for sure, and definitely want to learn more about it.

Anyway, here’s what I need. We have a database repository on our server, which holds our master versions of databases, which is set up as follows (here comes the lame ascii attempt at a folder structure):


What I would like is to have a script to go in each of the department folders, copying each file [database], and keeping the folder structure intact. However, I want the script to ignore all folders called “archived”, as well as the folder contents for these archives.

Ideally, I would like the script to work through any folders it finds automatically, rather than having to specify the folder names, as these could change, or could be added to. It should automatically disregard any folder called “Archived” (and any contents within these folders). Any files and folders found outside of these folders should be copied to another drive, keeping the folder structure in place.

Here’s the script I have so far. Any guidance would be appreciated… thank you in advance if you’re able to help.

Function DoBackup{


"Document Control",
"Drawing Office",
"Manuals & Tech Publications",

foreach ($folder in $folderlist)

Copy-Item -path $SourceFolder$folder -destination $TargetFolder -recurse -exclude “Archived”


} #End Function DoBackup

#Actually run the script…

Also - can you recommend any decent books to get started with scripting? Thanks :slight_smile:
by DonJ at 2012-09-26 08:03:59
Looks like a good start! Can you put this into the form of something specific, like "now, how do I add the part that does _____?" It’s a little easier to answer questions than just rewrite scripts ;). And you can ask as many questions as you need - let’s just tackle them one at a time.
by Adzzz at 2012-09-26 08:32:57
Hi Don, thank you.

First question then. The Copy-Item line is including the subfolder called “Archived” as well as its contents when it runs - i.e. it looks to me like the -exclude parameter is being ignored. How can I stop a folder from being copied (hiding the folder in explorer is one option I guess, but is there a way to force the script to ignore it?
by DonJ at 2012-09-26 08:48:10
There are a couple of ways. One would be to just filter them out. If -exclude isn’t doing what you want, use Where-Object:

Get-ChildItem | Where { $
.Name -notlike ‘archive’ }

Keep in mind that Get-ChildItem will retrieve both files and folders (in v3, there are switches to make it just retrieve one or the other). Always remember that you’re potentially dealing with both files AND folders.
by Adzzz at 2012-09-26 08:58:09
I’ll have a bash at that, thanks Don…