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

吃可爱长大的小学妹 提交于 2019-12-11 16:04:47

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/55727624/is-there-a-batch-file-command-line-or-powershell-that-will-assign-ownership-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!