Command line to permanently get access to folder

UPDATE While I was able to use the method described in accepted answer once it failed a second time or was just giving too much trouble.

Searching further I found SetACL by Helge Klein which is far far superior and does exactly what is needed.


I cannot access certain folders e.g. c:\Program Files\WindowsApps. Via command line I get an error of "Access is denied" and via Windows Explorer I get a pop up a dialog box stating "You don't currently have permission to access this folder"

enter image description here

Clicking on Continue gives me access, but I would like to grant myself access using the command line; so that my scripts do not run into this problem running under the same security context.

I found this question on Microsoft Technet but I couldn't get it to work. Note that I am already running Powershell as administrator.

PS C:\WINDOWS\system32> icacls "C:\Program Files\WindowsApps" /grant i063510:F /inheritance:e
C:\Program Files\WindowsApps: Access is denied.
Successfully processed 0 files; Failed processing 1 files
PS C:\WINDOWS\system32> icacls "C:\Program Files" /grant i063510:F /inheritance:e
C:\Program Files: Access is denied.
Successfully processed 0 files; Failed processing 1 files

If it matters, I am using Windows 10 upgraded from Windows 8.1.


Solution 1:

You don't currently have permission to access this folder

Try ICACLS and/or TAKEOWN with the below syntax against the folder with the username as needed.

Try just the ICACLS commands first and if that doesn't resolve, run the TAKEOWN commands and then run the ICACLS commands again.


ICACLS Commands

  • Grant explicit full control access to this folder to all beneath subfolders, and files leaving all inherited permissions in place and continue on error

    ICACLS "<FolderPath>" /INHERITANCE:e /GRANT:r <UserName>:(F) /T /C
    
  • Grant explicit modify access to this folder to all beneath subfolders, and files leaving all inherited permissions in place and continue on error

    ICACLS "<FolderPath>" /INHERITANCE:e /GRANT:r "<UserName>":(M) /T /C
    
  • Change the owner of this folder and all beneath subfolders and files and continue on error

    ICACLS "<FolderPath>" /SETOWNER "<UserName>" /T /C
    

TAKEOWN Commands

  • Run this as the account which you want to grant ownership to, and it'll be changed to the owner of the folder and all beneath subfolders and files

    TAKEOWN /F "<FolderPath>" /R /D Y