
PowerShellで
・空フォルダを一括で削除
できます!
※サブフォルダ配下の空フォルダも一括で削除できます。
コード
ここでは例として
・フォルダ「C:\Users\user\Desktop\test」配下の
・空フォルダを一括で削除
します。
※サブフォルダ配下の空フォルダも削除します。



# 対象フォルダ
$targetFolder = "C:\Users\user\Desktop\test"
$ws = New-Object -ComObject Wscript.Shell
try
{
# 指定フォルダ配下(サブフォルダ含む)のフォルダのリストを取得
$folderList = (Get-ChildItem $targetFolder -Directory -Recurse -ErrorAction Stop) | Sort-Object -Descending
# リストの数だけ繰り返し
foreach($folder in $folderList){
# フォルダ配下に何もない場合は削除
if ($folder.GetFileSystemInfos().Count -eq 0) {
Remove-Item -Force $folder.FullName
}
}
$ws.popup("空フォルダを削除しました。")
}
catch
{
$ws.popup("エラー : " + $PSItem)
}
実行結果
空フォルダを一括で削除できました。



参考
上記で使用した以下の詳細は、公式サイトをご確認ください。
●「Get-ChildItem」コマンドレット
●「Sort-Object」コマンドレット
●「Remove-Item」コマンドレット