Powershell 如何删除文件夹中的所有内容,但不删除子文件夹中包含所有内容的文件夹
我有一个这样结构的文件夹 MyFolder |----File1 |----File2 |----File... |----SubFolder01 |----SubFolder02 |----SubFolder... |----ImportantSubFolder |----VeryImportantSubSubFolder |----ImportantFile01 |----ImportantFile02 |----ImportantFile... |----ImportantFolder01... |----ImportantFolder... 我的文件夹 |----文件1 |----文件2 |----文件。。。 |----子文件夹01 |----子文件夹02 |----子文件夹。。。 |----重要文件夹 |----非常重要的子文件夹 |----重要文件01 |----重要文件02 |----重要文件。。。 |----重要的文件夹01。。。 |----重要的文件夹。。。 我需要删除“MyFolder”中的所有内容,但不删除“VeryImportantSubSubFolder”,包括此“VeryImportantSubfolder”中的所有文件/文件夹 所以删除后我只需要 MyFolder |----ImportantSubFolder |----VeryImportantSubSubFolder |----ImportantFile01 |----ImportantFile02 |----ImportantFile... |----ImportantFolder01... |----ImportantFolder... 我的文件夹 |----重要文件夹 |----非常重要的子文件夹 |----重要文件01 |----重要文件02 |----重要文件。。。 |----重要的文件夹01。。。 |----重要的文件夹。。。Powershell 如何删除文件夹中的所有内容,但不删除子文件夹中包含所有内容的文件夹,powershell,Powershell,我有一个这样结构的文件夹 MyFolder |----File1 |----File2 |----File... |----SubFolder01 |----SubFolder02 |----SubFolder... |----ImportantSubFolder |----VeryImportantSubSubFolder |----ImportantFile01 |----ImportantFile02
这应该可以做到:
$folder = 'C:\myfolder\'
Get-ChildItem -Path $folder | ForEach-Object {
if( !$_.PSIsContainer ) {
Remove-Item -Path $_.FullName -Force | Out-Null
}
}