如何使用 PowerShell 刪除空檔案和資料夾?


要刪除空檔案和資料夾,我們首先需要檢索列表,之前文章中已說明該步驟。

示例

在本文中,我們使用以下邏輯,即如果我們找到空檔案或資料夾,我們將刪除它們。要實現該邏輯,請使用以下指令碼。

gci C:\Temp -Recurse | foreach {
   if($_.Length -eq 0){
      Write-Output "Removing Empty File $($_.FullName)"
      $_.FullName | Remove-Item -Force
   }
   if( $_.psiscontainer -eq $true){
      if((gci $_.FullName) -eq $null){
         Write-Output "Removing Empty folder $($_.FullName)"
         $_.FullName | Remove-Item -Force
      }
}

上述命令將從 C:\temp 路徑中刪除空檔案和資料夾/子資料夾。

輸出

您將看到類似以下內容的輸出。


更新於:30-Mar-2021

3K+ 瀏覽量

啟動您的職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.