When you use PowerShell commands that involve managing files or folders, such as Remove-Item
, Get-Content
, Add-Content
, Move-Item
, or when trying to Export
something, like a CSV file, you might occasionally see an error that says “Access to the path is denied“. This usually means you don’t have the permissions you need to do what you want with a file or folder, but it can also happen when you run PowerShell as admin. In this guide, we’ll talk about what this error actually means, some examples of how it happens, and what you can do to fix it in your PowerShell scripts.
Also see: Get-AppxPackage is Not Recognized or Access is Denied
Page Contents
“Access to the path is denied” due to file and folder permissions in PowerShell
Most of the time, the “Access to the path is denied” error happens because of issues with file or folder permissions. If your script needs to touch a file or directory, it must have the right permissions to do so. This includes being able to read, write, and run things, which decides what you can do with the file or folder.
Example: Imagine you have a file called example.txt
in the C:\Data
directory. If you want to delete this file using the Remove-Item
cmdlet, the PowerShell script or the person running the script must have the delete permission for example.txt
.
Solution: To fix permission issues, you can change the permissions of the file or folder in question. Right-click on the file or folder, click ‘Properties‘, and go to the ‘Security‘ tab. Here, you can change the permissions to make sure the user or group running the PowerShell script has the right access.
For more complicated situations, you can use the icacls
command in PowerShell to change the permissions. For example, to give full control to the user Alvin
on example.txt
, you can use the following command instead.
icacls "C:\folder\example.txt" /grant Alvin:F
Having the right permission for your PowerShell session usually is the solution for the “Access to the path is denied” error when using Remove-Item
, Get-Content
, and Move-Item
commands in PowerShell.
Related resource: PowerShell Open URL in Specific Browser such as Chrome or Edge
PowerShell “Access to the path is denied” when files or folders are in use
Another big reason for the “Access to the path is denied” error in PowerShell is when the file or folder you want to work with is currently being used by another process. This stops PowerShell from doing things like deleting, moving, or reading the file.
Example: Let’s say you want to delete a log file (log.txt
) that is being written to by a program that’s still running. Trying to use Remove-Item
on log.txt
will lead to an access denied error because the file is locked by that program.
Solution: To sort this out, make sure no other processes are using the file or folder you want to work with. You can use tools like Process Explorer or the built-in Resource Monitor (just type resmon
in the Run dialog) to find and stop the process that’s locking the file.
Sometimes, just closing the app or service using the file is enough. If a system process or service is using it, you might need to stop that service for a while to do what you need in PowerShell.
Once you make sure the file or folder is free, try the Remove-Item
or Move-Item
command again in PowerShell and see if it works this time.
Useful guide: How to Reverse an Array in PowerShell
When you need to deal with “read-only” attributes
Files or folders that are set to read-only can also cause the “Access to the path is denied” error in PowerShell, especially when you try to change or delete them.
Example: If a file report.docx
is marked as read-only, trying to delete it with Remove-Item
or change its contents with Set-Content
will lead to an access denied error.
Solution: You can get rid of the read-only attribute using PowerShell or the file properties window. To do it with PowerShell, use the Attrib
command like this:
attrib -R "C:\folder\report.docx"
Or, you can right-click on the file, click ‘Properties‘, and uncheck the ‘Read-only‘ box in the Attributes section.
After you remove the read-only setting, you should be able to do what you need with the file or folder without running into the “Access to the path is denied” error.
Some things to note
The exact solutions to the “Access to the path is denied” error in PowerShell can vary based on what you are trying to do and the scripts you are trying to run.
If you keep seeing this error even after trying these solutions, try running the PowerShell Integrated Scripting Environment (ISE) as an administrator. This can give you a higher level of access that might solve permission issues not fixed by normal admin access to the normal PowerShell session. To do this, right-click on the PowerShell ISE icon and choose “Run as administrator.”