Skip to content

Instantly share code, notes, and snippets.

@chetan
Created September 12, 2012 03:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save chetan/3704149 to your computer and use it in GitHub Desktop.
Save chetan/3704149 to your computer and use it in GitHub Desktop.
Fix permissions on a roaming profile folder
REM usage: fix_perms.bat <username>
REM Recursively assign ownership to Administrators. Answer prompts with "Y".
takeown /R /A /F %1 /D Y
REM Grant Full permissions on folder and subfolders to Administrators, SYSTEM, and the user
cacls %1 /T /E /P "Administrators":F
cacls %1 /T /E /P SYSTEM:F
cacls %1 /T /E /P %1:F
REM Set owner back to UserName
subinacl.exe /noverbose /subdirectories %1\*.* /setowner=%1
@TobiKr
Copy link

TobiKr commented Dec 17, 2018

Find the Powershell script below:

Set-Location -Path "D:\Shares\Profile" #change to your profile root directory
$folders = Get-ChildItem

foreach($folder in $folders){
    Write-Host "Fixing Permissions for folder $folder" -ForegroundColor Yellow
    
    $user = $folder.ToString().Replace(".V2","").Replace(".V5","").Replace(".V6","")
    Write-Host "Username: $user" -ForegroundColor Cyan
    
    takeown /R /A /F $folder /D Y 

    cacls.exe $folder /T /E /P "Domain Admins:F" 
    cacls.exe $folder /T /E /P "SYSTEM:F"
    $caclsCmd = "cacls.exe " + $folder + " /T /E /P " + $user + ":F"
    Invoke-Expression $caclsCmd
    

    subinacl.exe /noverbose /subdirectories $folder /setowner=$user
    subinacl.exe /noverbose /subdirectories $folder\*.* /setowner=$user        
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment