3. File System Management Tasks - Powershell
3.99 Changing File and Folder Ownership
To set the owner of a folder, you can use the SetOwner method. For example, if you want to make ENTERPRISE\J.Carter the owner of the โSalesโ folder, you can run the following commands:
# Get the current ACL (Access Control List) for the folder
$acl = Get-Acl \\fs1\shared\sales
# Convert the user account name into a SID using the Ntaccount class
$object = New-Object System.Security.Principal.Ntaccount("ENTERPRISE\J.Carter")
# Set the specified user as the owner of the folder
$acl.SetOwner($object)
# Apply the updated ACL to the folder
$acl | Set-Acl \\fs1\shared\sales
Important Notes
- The
Ntaccountclass is used to convert the user account name from a string into a SID (Security Identifier). - The
SetOwnermethod allows you to change the owner of a folder, but the account must have the following permissions:- Take Ownership
- Read
- Change Permissions
Without these rights, the operation will fail.