Ok, so assuming that your folder structure is always the same, and you always want the 6th level of folder or subfolders of that, this will work:gci P:\ -directory -recurse | ?{$_.FullName -match ".:\\.+?\\.+?\\.+?\\.+?\\.+?\\" -and $_.CreationTime -lt (get-date).AddDays(-30)}|Remove-Item -recurse -whatif查看整个 P: 驱动器中的文件夹,如果文件夹名称是:That looks at the entire P: drive for folders, if the folder name is:P:\<anything>\<anything>\<anything>\<anything>\<anything>\<anything>它会检查它是否是在 30 多天前创建的,如果是,它会删除它及其所有内容.但它必须有这么多级别的脚本才能查看它.It checks to see if it was created over 30 days ago, and if so it deletes it and all of it's contents. But it has to have that many levels for the script to look at it.好的,让我们稍微改变一下.如果我们正在查找的文件夹始终称为30 Day Toss",我们将查找具有该名称的所有文件夹,然后拉出每个文件夹中的内容列表.够简单!一、代码: Ok, let's change it up a little. If the folder we're looking inside of is always called "30 Day Toss" we'll look for all folders with that name, and then pull a listing of things inside each of them. Easy enough! First, the code:gci P:\ -Directory -Recurse | ?{$_.Name -ieq "30 Day Toss"} | %{GCI $_.FullName | ?{$_.CreationTime -lt (get-date).AddDays(-30)} | Remove-Item -Recurse -Force}然后稍微分解一下.首先,我们获得 P: 驱动器上所有文件夹的列表.然后我们只选择名为30 天折腾"的那些(不区分大小写).First we get a listing of all folders on the P: drive. Then we select only the ones named "30 day toss" (not case sensitive).对于我们找到的每个具有该名称的文件夹,我们为该文件夹中的所有内容提取目录列表,然后仅选择超过 30 天的内容.对于超过 30 天的所有内容,我们都会删除它们,以及其中的任何内容.For each folder we find with that name we pull a directory listing for whatever is inside that folder, and then select only the things older than 30 days. For each of those things that are older than 30 days we delete them, and anything inside them. 这篇关于Powershell - 删除文件夹中特定名称超过 30 天的子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-28 07:56