Permission errors in PowerShell

Powershell

Powershell Problem Overview


I am new to PowerShell. When trying to write a simple script that deletes the contents of a folder and then fills it with files copied from a different folder, I always get a PermissionDenied error.

Details:

+ remove-item <<<<  D:\path\* -recurse
    + CategoryInfo : PermissionDenied: (save.gif:FileInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand

Where is the problem? I am able to manipulate both folders through Explorer. The error occurs both when running from a script file and from shell (using Windows PowerShell ISE). The ISE process runs under my account. I'm running Windows 7 Professional and am a local administrator.

Edit: After Richard's suggestion, I tried the verbose mode (which seemed to have no effect).

PS Z:\> $error[0] | fl * -force

PSMessageDetails      : 
Exception             : System.IO.IOException: Not Enough permission to perform operation.
TargetObject          : D:\path\file.txt
CategoryInfo          : PermissionDenied: (D:\path\file.txt:FileInfo) [Remove-Item], IOException
FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
ErrorDetails          : Cannot remove item D:\path\file.txt: Not Enough permission to perform operation.
InvocationInfo        : System.Management.Automation.InvocationInfo
PipelineIterationInfo : {0, 1}

I don't see anything of much use there (but thanks for the tips anyway).

Edit 2: Okay, here's the script source:

remove-item D:\path_A\* -recurse
copy-item D:\path_B\* D:\path_A\

That's it. The remove-item seems to throw at every file.

Powershell Solutions


Solution 1 - Powershell

Have you try :

remove-item D:\path_A\* -recurse -force

Solution 2 - Powershell

Is UAC enabled? If so try running your PowerShell session as 'Administrator'. It really looks like you don't have permission to delete the objects.

We have very restrictive security policies where I work and users not familiar with UAC get burned all the time.

Solution 3 - Powershell

In addition to reasons mentioned in the above posts, i've observed that "Access Denied" error is thrown when the file is being accessed by a separate process (In my case, i had to stop the server before Rename-Item could be run successfully).

Solution 4 - Powershell

After this error (and assuming at that point in it the most recent error):

$error[0] | fl * -force

will expand the details of the error and exception. That should give you more information.

Another thing to do is to switch on verbose logging

$VerbosePreference = "Continue"

to get more details about specifically what operation is being performed when the error occurs.

Finally PowerShell ISE includes a debugger which allows you to step through your script.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionverView Question on Stackoverflow
Solution 1 - PowershellJPBlancView Answer on Stackoverflow
Solution 2 - PowershellGreg WojanView Answer on Stackoverflow
Solution 3 - PowershellaazeemView Answer on Stackoverflow
Solution 4 - PowershellRichardView Answer on Stackoverflow