Shell – Can’t Remove Folder with Powershell But Can with Command Prompt

file-permissionspowershell

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?

Best Answer

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.

Related Question