Take Ownership of all Files and Folders
The first script takes ownership of all the files and folders in our archive folder:
takeown /a /r /d Y /f '\\my-nas-drive\archive\jobs\'
You can find more information about the command takedown
at https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/takeown
But basically this command gives ownership to the Administrator group /a
, performs a recursive operations /r
, suppresses any confirmation prompt /d Y
, for the specified path /f
Reset Permissions
Our second script fetches every folder in our $FolderPath
(line 3
), loops through each folder (line 5
), fetches the Access Control List ($acl
line 10
) for the object. Then the key process are lines 11-12
where we turn on permission inheritance and remove the current permission on the folder.
We repeat this process again but for all the files in the current folder (lines 18-20
)
clear
$FolderPath = '\\my-nas-drive\archive\jobs\'
$folders = Get-ChildItem -Path $FolderPath -Directory -Recurse
foreach ($folder in $folders)
{
Write-Host "Directory: " -ForegroundColor Yellow -NoNewline
Write-Host $folder.FullName -ForegroundColor Green
$acl= get-acl -Path $folder.FullName
$acl.SetAccessRuleProtection($false,$true)
Set-Acl -Path $Folder.FullName -AclObject $acl
$files = Get-ChildItem -Path $folder.FullName -File
foreach ($file in $files){
Write-Host " " $file.FullName -ForegroundColor Gray
$acl= get-acl -Path $file.FullName
$acl.SetAccessRuleProtection($false,$true)
Set-Acl -Path $File.FullName -AclObject $acl
}
}
NOTE: This script is not quick. It took over 18 days to processes our 30TB of data and three million files!