用于删除以“.”开头的文件夹的 Powershell 脚本 不起作用 - 为什么?

XDS*_*XDS 2 windows shell powershell scripting

我编写了一个简单的 powershell 脚本,旨在删除特定文件夹:

gci -include .vs -recurse | remove-item -force -recurse
Run Code Online (Sandbox Code Playgroud)

但是 .vs 文件夹不会被删除(如果点被删除,那么名为“vs”的文件夹就会被删除)。我肯定错过了什么。

$PSVersionTable.PSVersion
Run Code Online (Sandbox Code Playgroud)

返回报告:

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      16299  666
Run Code Online (Sandbox Code Playgroud)

我通过文件资源管理器“右键单击 -> 运行 Powershell 脚本”运行脚本。不知道这是否会使脚本在最新最好的 powershell 版本下运行。

更新:

事实证明,罪魁祸首是 .vs 文件夹被标记为“只读”。由于某种原因,即使确实指定了“-force”标志,powershell 脚本也无法删除它。对此有什么可以做的吗?

mkl*_*nt0 5

您在评论中提到要删除的目录同时设置了HiddenReadOnly文件系统属性

虽然-Force在您的Remove-Item调用中能够强制删除具有该ReadOnly属性的项目,但您的输入 Get-ChildItem调用-Force也需要,否则它将找不到隐藏的文件和文件夹,因此Remove-Item也永远不会看到它们:

# Note the -Force added to Get-ChildItem.
Get-ChildItem -Force -Include .vs -Recurse | Remove-Item -Force -Recurse -WhatIf
Run Code Online (Sandbox Code Playgroud)