简体   繁体   English

如何删除使用 powershell 在路径中创建的最后一个目录及其子文件夹

[英]how to delete last directory and its subfolders that created in a path using powershell

i want to delete a last directory of a path using powershell this directory maybe have subfolders and i want to delete all of them.我想使用 powershell 删除路径的最后一个目录,该目录可能有子文件夹,我想删除所有子文件夹。 eg in drive "D:" my last folder is "New Folder" and "New Folder" have subdirectories eg "A" and "B" and "C" i want to define paht of drive "D:" in powershell script and powerhshell check my last created directory that is "New Folder" and delete "New Folder" with all of that subdirectories.例如在驱动器“D:”中,我的最后一个文件夹是“新文件夹”,“新文件夹”有子目录,例如“A”和“B”和“C”我想在 powershell 脚本和 powerhshell 中定义驱动器“D:”的路径检查我最后创建的目录“新文件夹”并删除所有子目录的“新文件夹”。 i will be thank you that help me.我会感谢你帮助我。 thank you.谢谢你。

This task is readily broken into two steps:此任务很容易分为两个步骤:

  1. Discover the newest directory发现最新目录
  2. Remove it (and any items it contains)删除它(以及它包含的任何项目)

We can discover the newest directory by enumerating all directories with Get-ChildItem -Directory , then sorting them based on their CreationTime property, and finally selecting only the newest:我们可以通过使用Get-ChildItem -Directory枚举所有目录来发现最新的目录,然后根据它们的CreationTime属性对它们进行排序,最后只选择最新的:

$path = "D:\"

$newestDirectory = Get-ChildItem $path -Directory |Sort-Object CreationTime -Descending |Select-Object -First 1

To remove the directory along with any contents without being prompted for confirmation, use Remove-Item -Recurse -Force :要在不提示确认的情况下删除目录以及任何内容,请使用Remove-Item -Recurse -Force

$newestDirectory |Remove-Item -Recurse -Force

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM