Remove *.File Access to path is denied

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Byron Grogan Byron Grogan 1 year, 11 months ago.

  • Author
    Posts
  • #21073
    Profile photo of Byron Grogan
    Byron Grogan
    Participant

    I am trying to remove files on a file server / virtual SMTP server with file extension *.bad to remove all bad mail + other files to free up some disk space. When I run the cmdlet PS responds with an access error. I have also ran the Set-ExecutionPolicy Unrestricted and run as administrator and ACL is correct.

    {PS C:\> Get-ChildItem C:\ -Include *.bad -Recurse | ForEach-Object ($_) {Remove-Item $_.fullname}
    Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\WMI\RtBackup' is denied.
    At line:1 char:14
    + Get-ChildItem < <<<  C:\Windows\System32\LogFiles\WMI\RtBackup -Include *.bad -Recurse | ForEach-Object ($_) {Remove-Item $_.fullname}
        + CategoryInfo          : PermissionDenied: (C:\Windows\Syst...es\WMI\RtBackup:String) [Get-ChildItem], UnauthorizedAccessException
        + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand}
  • #21075
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    Couple things: What's with the '($_)' after Foreach-Object? The Process parameter should be right after the cmdlet name. Also, you can pipe Get-Childitem directly to Remove-Item without using foreach-object.

    However, that is not the cause of your error since the command is failing on Get-Childitem first.

    Are you certain that the user that launched the Powershell session has access to the folder? You are getting an access denied for one particular folder – C:\Windows\System32\LogFiles\WMI\RtBackup. If you navigate directly to that chances are you will be prompted for elevated privileges.

    Lastly, removing files like this from the root drive seems like a bad idea. Are you certain this is what you want to do?

    • #21079
      Profile photo of Byron Grogan
      Byron Grogan
      Participant

      I am new to PowerShell and got that cmdlet from http://stackoverflow.com/. However, I am logged in as the local administrator account. I don't understand the

      {($_)}

      either. However I did manage to run the cmdlet successfully by piping Get-ChildItem to Remove-Item

      {Get-ChildItem C:\inetpub\mailroot\ -Include *.bad -Recurse | Remove-Item}

      as you suggested. I am sure that I can remove the files, but I think specifying the entire path would be a safe option, rather than the entire system drive?

    • #21081
      Profile photo of Byron Grogan
      Byron Grogan
      Participant

      Yes you were correct, I was prompted for elevated privileges to access that folder. The access denied error was preventing the cmdlet from executing successfully but as you said removing files like this from the root drive may be a bad idea..

  • #21077
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    That's normal anytime you run Get-ChildItem on the entire system drive. There are a number of locations that will pop up an "Access Denied" error for various reasons (even if you're running as an administrator.) Junction points and such that cause some odd behavior. If you really want to do this, I'd just throw an -ErrorAction Ignore (v3+) or -ErrorAction SilentlyContinue (v2) onto the Get-ChildItem command to suppress those errors.

You must be logged in to reply to this topic.