PowerShell Change owner of files and folders

前端 未结 2 1758
孤城傲影
孤城傲影 2021-02-19 22:18

Searching the web, I found 2 scripts that are able to change the owner of files and folders. When testing this, it functions perfectly in PowerShell 1.0. Now I\'m trying to comb

2条回答
  •  一向
    一向 (楼主)
    2021-02-19 22:19

    You can use the SetOwner() method for folders, just like for files.

    # Define the owner account/group
    $Account = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList 'BUILTIN\Administrators';
    
    # Get a list of folders and files
    $ItemList = Get-ChildItem -Path c:\test -Recurse;
    
    # Iterate over files/folders
    foreach ($Item in $ItemList) {
        $Acl = $null; # Reset the $Acl variable to $null
        $Acl = Get-Acl -Path $Item.FullName; # Get the ACL from the item
        $Acl.SetOwner($Account); # Update the in-memory ACL
        Set-Acl -Path $Item.FullName -AclObject $Acl;  # Set the updated ACL on the target item
    }
    

提交回复
热议问题