Setting file ownership in PowerShell · Tue Jan 8, 10:24 PM

My latest target of investigations — Windows PowerShell — is pretty spiffy. Today I learned one way (I’m sure there are easier ways) to set the owner for a directory’s files to the “Administrators” group, to wit:

$fix = (Get-ChildItem | Get-Acl | ?{ $_.Owner -ne ‘BUILTIN\Administrators’ })
$acct = New-Object System.Security.Principal.NTAccount(“Administrators”)

foreach ($f in $fix) { $f.SetOwner($acct); (Get-Item $f.Path).SetAccessControl($f); }

-- David --

...