Joe Jourdan
Joe Jourdan

Reputation: 1

Is there a batch-file, command line, or powershell, that will assign ownership of a file to whomever you want?

Repeating the title: Is there a batch-file, command line, or powershell, that will assign ownership of a file to whomever you want? I have been deploying new PC's at the start of a rollout. Machines are being setup for different groups one batch at a time. I need a batch, command, or powershell to assign ownership for certain folders. I have tried a few different batch-files but to no avail. I use this:

takeown /f "c:\program files" /r /d y

it works fine. Then I try this:

icacls "c:\program files" /setowner "LocalAdmin"

but nothing happens. I am not opposed to using any of the 3 (.bat, cmd, or ps). I just need something simple that works. I figure this is the place to ask. You guys have helped me so many times, by answering other questions for people. I am forever grateful.

Upvotes: 0

Views: 436

Answers (1)

boxdog
boxdog

Reputation: 8432

You can set the owner of a file using the Get-Acl and Set-Acl cmdlets. For example, here's how to assign ownership to the local 'Administrators' group:

# Get the 'Administrators' identity
$adminGroup = New-Object System.Security.Principal.NTAccount('Administrators')

# Get the existing ACL for the file
$fileACL = Get-Acl -Path "C:\TargetFile.txt"

# Set the new owner in the ACL - inmemory only
$fileACL.SetOwner($adminGroup)

# Write updated ACL back to file
Set-Acl -Path "C:\TargetFile.txt" -AclObject $fileACL

Upvotes: 1

Related Questions