powershell复制所有文件夹结构并排除一个或多个文件夹

时间:2018-03-26 13:06:26

标签: powershell

我正在尝试使用PowerShell将包含来自用户的子文件夹的文件夹复制到小型备份。这些文件夹包含一个名为“windows”的文件夹,我不想复制。

我尝试过“排除”但似乎无法让它发挥作用。 到目前为止这是脚本:

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

我已阅读其他帖子,但不清楚了解它是如何运作的

这是我第一次使用PowerShell

2 个答案:

答案 0 :(得分:0)

我使用这样的东西:

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

我还没有对它进行测试,但它是你应该能够工作的骨架,虽然我没有找到在两者上使用recurse的重点,Get-ChildItem和Copy - 我的建议是在Get-ChildItem上使用它。

答案 1 :(得分:0)

你是完全正确的。 实际上这个脚本比我以前写的那个简单。 我们走了:

$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
}