簡體   English   中英

Powershell復制所有文件夾結構並排除一個或多個文件夾

[英]powershell copy all folder structure and exclude one or more folders

我正在嘗試使用PowerShell將包含子文件夾的文件夾從我們的用戶復制到小型備份中。 這些文件夾包含一個我不想復制的名為“ windows”的文件夾。

我嘗試過“排除”,但似乎無法使其正常工作。 到目前為止,這是腳本:

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

我看過其他帖子,但不安靜地了解它的工作原理

這是我第一次使用PowerShell

我會用這樣的東西:

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

我尚未對其進行測試,但這是您應該能夠進行工作的基礎,盡管我沒有發現在Get-ChildItem和Copy-Item上都使用遞歸的意義,我的建議是在Get- ChildItem。

你是完全正確的。 實際上,該腳本比我之前編寫的腳本更簡單。 開始了:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM