ZeroSharp

Blog

How to recursively change file ownership

I recently ran into some file ownership trouble after cloning a bitbucket repository.

The following script saved my bacon.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# FixOwnership.ps1
# This script recursively fixes the ownership on the files in the 
# current and subdirectories.

$acct1 = New-Object System.Security.Principal.NTAccount('Administrators')
$profilefolder = Get-Item .
$acl1 = $profilefolder.GetAccessControl()
$acl1.SetOwner($acct1)
dir -r . | set-acl -aclobject $acl1
pause