Skip to content
Access to the path '...' is denied

Access to the path '...' is denied

DodaTech 3 min read

The “Access to the path ‘…’ is denied” error means your process lacks permissions to read, write, or delete the specified file or directory.

What It Means

Windows uses a discretionary access control system where every file and folder has an ACL (access control list) specifying which users and groups can perform which actions. PowerShell runs under your user account’s security context. When your account does not have the required permission — or the file is locked by another process — the operating system denies the operation and PowerShell surfaces the .NET UnauthorizedAccessException.

Why It Happens

  • You do not have read or write permissions for the target file or folder.
  • The file is marked read-only or is a system file.
  • Another process (antivirus, another PowerShell window, a text editor) has the file locked.
  • PowerShell is not running with administrator privileges and the path is protected (e.g., C:\Windows\System32).
  • The file attributes include Hidden or System, and your script does not handle them.
  • The file is encrypted with EFS and your user does not have the decryption key.

How to Fix It

1. Run PowerShell as Administrator

# Right-click PowerShell and select "Run as administrator"
# Or from an existing admin session:
Start-Process powershell -Verb RunAs

2. Check and modify file permissions

# Check current permissions
Get-Acl -Path "C:\Protected\file.txt" | Format-List

# Grant full access to your user (run as admin)
$acl = Get-Acl "C:\Protected\file.txt"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
    "$env:USERDOMAIN\$env:USERNAME",
    "FullControl",
    "Allow"
)
$acl.SetAccessRule($accessRule)
Set-Acl -Path "C:\Protected\file.txt" -AclObject $acl

3. Remove read-only attribute

Set-ItemProperty -Path "C:\Path\to\file.txt" -Name IsReadOnly -Value $false

4. Unlock the file by closing the holding process

# Find which process has the file locked
# Use Sysinternals Handle or:
Get-Process | Where-Object {
    $_.Modules.FileName -match "filename"
}

5. Take ownership of the file

# Run as Administrator
takeown /F "C:\Protected\file.txt"
icacls "C:\Protected\file.txt" /grant "$env:USERNAME`:F"

6. Bypass inaccessible directories with error handling

Get-ChildItem -Path "C:\Windows" -Recurse -ErrorAction SilentlyContinue
Does running as Administrator grant access to everything?
No — even an administrator must explicitly take ownership of some system files (e.g., C:\Windows\System32\config). The SYSTEM account and TrustedInstaller have higher privileges. Use takeown and icacls to transfer ownership before modifying protected system files.
How do I check which user I’m running as in PowerShell?
Run whoami or $env:USERNAME. To check for administrator privileges specifically, use ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) which returns True or False.
Can I delete a file without modifying permissions?
Yes — in some cases you can bypass the ACL by changing ownership first. The takeown /F path command assigns ownership to the current user (requires admin), after which you can modify permissions or delete the file. For locked files, you must first terminate or close the holding process.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro