2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Microsoft] 空フォルダを全削除するPowerShellスクリプト - Remove empty folders recursively.

Last updated at Posted at 2020-03-31

robocopy /purgeは、ファイルは消してくれるけれどディレクトリは消してくれません。
アプリをアンインストールするとAppDataの下に空フォルダが残ります。
それらを一括して消したいとき用のスクリプトです。

深さ優先で消してますので、消し残しありません。

4/3 追記
初出時は、隠しディレクトリ/ファイルを消せていませんでした。

スクリプト本体

Function Remove-EmptyFolder {
    [cmdletbinding()]
    Param(
        [parameter(mandatory=$true)][String]$Path
    )

    Process {
        (Get-ChildItem -Force $Path | ? { $_.PSIsContainer -eq $true }) | % { Remove-EmptyFolder $_.FullName }
                
        $dir = Get-Item -Force $Path
       
        if ($dir.PsIsContainer -and $dir.GetFileSystemInfos().Count -eq 0) {
            Remove-Item -Force $dir
        }
    }
}

notepad $profile としてファイルを開いてコピペしてあげれば、PowerShell起動時に読み込まれるようになります。

使い方

Remove-EmptyFolder フォルダのパス
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?