0

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.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Surfin
  • 1
  • 2
    What is the problem you are having with file names with brackets? – Lars Truijens Sep 09 '13 at 20:41
  • I had a problem with brackets but I think it has been solved with `$f.GetAccessControl()` Actually, I have this error message : `Set-Acl the Security Identifier Is Not Allowed to Be the Owner of This Object` with files and folder that matche with the owner I search. – Surfin Sep 10 '13 at 09:08

0 Answers0