首页 > 解决方案 > 用于删除以“。”开头的文件夹的 Powershell 脚本 不起作用 - 为什么?

问题描述

我整理了一个简单的 powershell 脚本,用于删除特定文件夹:

gci -include .vs -recurse | remove-item -force -recurse

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

$PSVersionTable.PSVersion

回报告:

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      16299  666

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

更新:

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

标签: windowsshellpowershellscripting

解决方案


您在评论中提到要删除的目录同时具有HiddenReadOnlyfilesystem 属性集

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

# Note the -Force added to Get-ChildItem.
Get-ChildItem -Force -Include .vs -Recurse | Remove-Item -Force -Recurse -WhatIf

推荐阅读