1
votes

I'm trying to use PowerShell to copy a folder with sub-folders from our users to a small backup. These folders contain a folder called "windows" I don't want to copy.

I have tried "exclude" but can't seem to get it to work. This is the script so far:

Copy-Item "E:\Curos folder" -Exclude 'Windows' -Destination "E:\Curos folder backup" -Recurse -Verbose

I have read other posts but don't quiet understand how it works

It's my first time working with PowerShell

2
"can't seem to get it to work" <- what's happening? Are you getting errors/are the files copying at all/etc?gvee

2 Answers

1
votes

You are complete right. Actually the script it's simpler than the one I have wrote before. Here we go:

$source =  "C:\Users\gaston.gonzalez\Documents\02_Scripts"
$destination = "D:\To Delete"

$exclude = "Windows"
$folders = Get-ChildItem -Path $source | Where {($_.PSIsContainer) -and ($exclude -notcontains $_.Name)}


foreach ($f in $folders){ 
      Write-Host "This folders will be copied: $f"
      Copy-Item -Path $source\$f -Destination $destination\$f -Recurse -Force
} 
0
votes

I'd use something like this:

Get-ChildItem $root -Directory -Recurse | % {$_.name -ne 'Windows'} | foreach {Copy-Item "$($_.FullName)" -Destination $dest -Recurse}

I haven't tested it but it's the skeleton of something you should be able to make work, although I don't find the point of using recurse on both, Get-ChildItem and Copy-Item my advice is to use it on Get-ChildItem.