PowerShell删除最后一个子文件夹

PowerShell删除最后一个子文件夹,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,有没有办法使用PowerShell仅删除最后一个空文件夹? 示例:我有一个文件夹结构 。-main文件夹 子文件夹1 a b c 子文件夹2 a b 每晚使用robocopy,我都会将所有内容复制到另一台服务器上,之后我应该删除所有最后的子文件夹(a、b、c等…) 使用/MUVE,它会删除“子文件夹1”和“子文件夹2”,但它们应该留在那里 (如果我删除文件夹“a”、“b”、“c”,“子文件夹1”也为空,因此我无法删除所有空文件夹。) 我不能使用/FX,也不知道文件夹的名称,只知道根目录路径“C:

有没有办法使用PowerShell仅删除最后一个空文件夹?
示例:我有一个文件夹结构

。-main文件夹
子文件夹1
a
b
c
子文件夹2
a
b

每晚使用robocopy,我都会将所有内容复制到另一台服务器上,之后我应该删除所有最后的子文件夹(a、b、c等…

使用/MUVE,它会删除“子文件夹1”和“子文件夹2”,但它们应该留在那里
(如果我删除文件夹“a”、“b”、“c”,“子文件夹1”也为空,因此我无法删除所有空文件夹。) 我不能使用/FX,也不知道文件夹的名称,只知道根目录路径“C:\SharedFolders\”。而且我知道应该删除的文件夹处于第3级。

您可以使用带有
-Directory
开关的cmdlet检索所有文件夹,使用cmdlet筛选空文件夹,最后使用以下命令删除文件夹:

这将仅删除最后一个空文件夹,结果:

..-mainFolder 
...................-subFolder1 
..................-subFolder2 
您可以将cmdlet与
-Directory
开关一起使用,以检索所有文件夹,使用cmdlet筛选空文件夹,最后使用以下命令删除文件夹:

这将仅删除最后一个空文件夹,结果:

..-mainFolder 
...................-subFolder1 
..................-subFolder2 

您可以使用
Get ChildItem-Recurse
检索所有文件夹,然后调用目录对象上的
GetFiles()
GetDirectories()
方法来确定它们是否为空:

$EmptyDirs = Get-ChildItem C:\path\to\mailFolder -Directory -Recurse | Where {-not $_.GetFiles() -and -not $_.GetDirectories()}
# and then remove those
$EmptyDirs | Remove-Item

您可以使用
Get ChildItem-Recurse
检索所有文件夹,然后调用目录对象上的
GetFiles()
GetDirectories()
方法来确定它们是否为空:

$EmptyDirs = Get-ChildItem C:\path\to\mailFolder -Directory -Recurse | Where {-not $_.GetFiles() -and -not $_.GetDirectories()}
# and then remove those
$EmptyDirs | Remove-Item