這個問題在這里已經有了答案: 如何在 Powershell 中自動回答“是”? (1 個回答) 2天前關閉。
我想洗掉臨時檔案夾中的所有檔案...任何 .zip 檔案、.txt 檔案和任何檔案夾檔案,包括每個檔案夾中的任何內容(所有內容)。我認為這很簡單,但到目前為止,我的腳本不斷收到確認彈出視窗,詢問我是否要洗掉所有這些子項。我嘗試使用-confirm:$false,但這似乎不起作用。我很感激任何建議。謝謝你。
$list = Get-ChildItem -directory "C:\temp\*" -Include * -Name
get-childitem c:\temp -Include @(get-content $list) | Remove-Item -Force -whatif
我嘗試使用-confirm:$false引數以及-force沒有運氣。
uj5u.com熱心網友回復:
你想要-path vs -directory。
#Looking for Temp under Windows:
$list = (Get-ChildItem -path "C:\*\Temp*\*.*").FullName | Remove-Item -Force
#Looking for Temp under Root:
$list = (Get-ChildItem -path "C:\Temp*\*.*").FullName | Remove-Item -Force
如果您的 Temp 目錄有 subs,您還可以添加 -recurse 開關。
uj5u.com熱心網友回復:
為避免確認請求添加引數-confirm:$false。如果要包含所有內容,請不要指定引數-include *- 默認是回傳所有內容,無需因不必要的過濾器而減慢速度。
做就是了:
get-childitem -path C:\temp -directory | remove-item -force -confirm:$false -recurse
順便提一句。您確實指定了引數-directory,目前get-childitem僅回傳目錄,因此直接存盤在C:\ temp中的檔案將保留。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/523425.html
