XDS
XDS

Reputation: 4188

Powershell script for deleting folders starting with a '.' doesn't work - why?

I've put together a simple powershell script meant to delete specific folders:

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

However the .vs folders don't get deleted (if the dot is removed then folders named 'vs' get removed just fine). I must be missing something.

$PSVersionTable.PSVersion

Reports back:

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

I run the script through the file-explorer ala 'Right Click -> Run Powershell Script'. Don't know if this makes the script run under the latest and greatest version of powershell or not.

Update:

Turns out that the culprit is that the .vs folder is marked as "read-only ". For some reason the powershell script can't delete it even the '-force' flag is indeed specified. Is there anything that can be done about this?

Upvotes: 2

Views: 1004

Answers (1)

mklement0
mklement0

Reputation: 437688

You mention in a comment that the directories to remove have both the Hidden and the ReadOnly filesystem attribute set.

While -Force in your Remove-Item call is capable of forcing removal of items that have the ReadOnly attribute, your input Get-ChildItem call requires -Force too, otherwise it won't find hidden files and folders, so Remove-Item never sees them either:

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

Upvotes: 5

Related Questions