How to change owner recursivly depending of the actual owner

111 Views Asked by At

I'm just a beginer with PowerShell but I'm already sure that it's a powerfull language. I want to make an easy thing with my first script : "Recursivly changing owner of files and folder depending of the actual owner". But I realize that it will not be as easy as I imagined. One of my problems is that some files or folder name have [brackets].

Here's my script :

$FolderToScan = "D:\Sauvegardes"
$OldOwner = "BUILTIN\Administrateurs"
$NewOwner = New-Object System.Security.Principal.NTAccount("MON-Domaine","MON-Utilisateur")

$files = Get-ChildItem -LiteralPath $FolderToScan -Recurse

Foreach ($file in $files)
{
    $f = Get-Item -LiteralPath $file.FullName
    $f = $f.GetAccessControl()
    If ($f.Owner -eq $OldOwner) {
        $f.SetOwner($NewOwner)
        Set-Acl -path $file.FullName -aclObject $f
    }
}

What do you thing about it ? Please help.

0

There are 0 best solutions below