Remove *.File Access to path is denied

This topic contains 4 replies, has 3 voices, and was last updated by  Byron Grogan 2 years, 8 months ago.

  • Author
  • #21073

    Byron Grogan

    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\\WMI\RtBackup:String) [Get-ChildItem], UnauthorizedAccessException
        + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand}
  • #21075

    Matt McNabb

    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

      Byron Grogan

      I am new to PowerShell and got that cmdlet from 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

      Byron Grogan

      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

    Dave Wyatt

    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.