3

I'm trying to delete a folder in an install script using PowerShell. The script copies a bunch of files to a distribution folder (using RoboCopy) and then cleans up superfluous files and folders.

I seem to be unable to use PowerShell to delete a folder name c:\installs\wwhelp\Editors\.vs even after having set permissions in the Powershell Console running as admin.

I essentially do this after copying and cleaning out the .vs folder:

set source=\wwapps\wwhelp
set target=\installs\wwhelp
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy UnRestricted

robocopy $source\Editors $target\Editors /MIR
deletefiles $target\Editors\.vs\*.* -r -f   <-- deletes files & folder recursively
rd  $target\Editors\.vs

This fails with:

rd : Cannot remove item C:\installs\wwhelp\editors\.vs: You do not have sufficient access rights to perform this operation.

The folder is empty and I'm running as admin and I've set the execution policy. If I create a folder called vs in the same folder structure the remove works.

Likewise running the rd command in the command window with Admin works.

Any ideas why I get a security exception on a folder that starts with a . in Powershell?

7
  • .vs is a folder? Somewhere the \ seems to have gone missing. Can you try rd c:\\installs\\wwhelp\\editors\\.vs Jan 19, 2016 at 5:42
  • @LievenKeersmaekers You don't need to escape \ when using rd
    – DavidPostill
    Jan 19, 2016 at 14:35
  • @DavidPostill - the folder is \.vs but the error message shows editors.vs. That's actually all I was referring to. Jan 19, 2016 at 15:13
  • 1
    @LievenKeersmaekers It doesn't matter. md f:\test\.vs followed by rd f:\test\.vs works as expected with no error messages - in both cmd and powershell. The OP has a different problem than escaping anything.
    – DavidPostill
    Jan 19, 2016 at 15:29
  • I can't argue with that. If OP should happen to read your comment and provide additional information I'll remove my bogus comments. Jan 19, 2016 at 19:18

2 Answers 2

4

To remove files/folders that start with ., you have to use the -Force parameter.

That's because PowerShell is treating them as read-only/hidden.

1

Had similar problem trying to delete a project I created in wrong folder with dotnet CLI. In my case .vs is a hidden folder, so I used rd with -force switch to remove it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.