无法移除项目.目录不为空 [英] Cannot remove item. The directory is not empty

查看:50
本文介绍了无法移除项目.目录不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除包含子文件夹/文件的文件夹.

I am trying to delete a folder with subfolders/files.

Remove-Item -Force -Recurse -Path $directoryPath

我收到错误 无法删除项目.目录不为空.

我的 PowershellScript.ps1 具有 executionPolicy 不受限制.我尝试使用当前登录用户删除的根文件夹对该文件夹具有完全权限.

My PowershellScript.ps1 has executionPolicy unrestricted. The root folder I try to delete with the current logged in user has full permission on this folder.

在我的本地电脑上,代码有效,但在我的 Windows Server 2012 R2 上无效.

On my local pc the code works but not on my Windows Server 2012 R2.

推荐答案

您可以尝试以下操作:

Remove-Item -Force -Recurse -Path "$directoryPath\*"

请注意,在 Remove-Item 中将 -Recurse 参数与 -Include 一起使用时,它可能不可靠.所以最好先用 Get-ChildItem 递归文件,然后通过管道输入 Remove-Item.如果您删除大型文件夹结构,这也可能有所帮助.

Note when using the -Recurse parameter with -Include in Remove-Item, it can be unreliable. So it's best to recurse the files first with Get-ChildItem and then pipe into Remove-Item. This may also help if you deleting large folder structures.

Get-ChildItem $directoryPath -Recurse | Remove-Item -Force   

这篇关于无法移除项目.目录不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆