Link to home
Start Free TrialLog in
Avatar of CaussyR
CaussyR

asked on

Take Ownership of Folders

Please, could anyone let me know how I can change a folders ownership in Powershell ? I have tried the following :

$FTPPath = "c:\test"
$NewFTPUser = 'TESTFTP'
Get-Acl $FTPPath | Format-List
$ACL = Get-Acl $FTPPath
$Rule = New-Object System.Security.AccessControl.FileSystemAccessRule("TESTFTP","FullControl")
$ACL.AddAccessRule($Rule)
Set-Acl $FTPPath $ACL

The above works for adding permissions to a folder but need to know how to take ownership.

Thanks
Avatar of Patrick Bogers
Patrick Bogers
Flag of Netherlands image

Hi,

This peace of code should do some magic.

$acl = (Get-Item $path).GetAccessControl("Access")

# Setup the access rule.
$allInherit = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit", "ObjectInherit"
$allPropagation = [System.Security.AccessControl.PropagationFlags]"None"
$AR = New-Object System.Security.AccessControl.FileSystemAccessRule $user, $permissions, $allInherit, $allPropagation, "Allow"

# Check if Access already exists.
if ($acl.Access | Where { $_.IdentityReference -eq $User})
{
    $accessModification = New-Object System.Security.AccessControl.AccessControlModification
    $accessModification.value__ = 2
    $modification = $false
    $acl.ModifyAccessRule($accessModification, $AR, [ref]$modification) | Out-Null
}
else
{
    $acl.AddAccessRule($AR)
}

Set-Acl -AclObject $acl -Path $Path
Avatar of CaussyR
CaussyR

ASKER

Could you let me know what the following variables should be set to :

$user, $permissions, $allInherit, $allPropagation

I am also seeing the following error :

Get-Acl : A positional parameter cannot be found that accepts argument 'System.Security.AccessControl.DirectorySecurity'.
At line:3 char:1
+ acl = (Get-Item $path).GetAccessControl("Access")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Acl], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetAclCommand
 
New-Object : Cannot find an overload for "FileSystemAccessRule" and the argument count: "5".
At line:8 char:7
+ $AR = New-Object System.Security.AccessControl.FileSystemAccessRule $user, $perm ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
 
Exception calling "AddAccessRule" with "1" argument(s): "Value cannot be null.
Parameter name: rule"
At line:20 char:5
+     $acl.AddAccessRule($AR)
+     ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException
 
Set-Acl : The security identifier is not allowed to be the owner of this object.
At line:23 char:1
+ Set-Acl -AclObject $acl -Path $Path
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (C:\Temp\Test:String) [Set-Acl], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.SetAclCommand
 


Do I need any particular module loaded in Powershell ?
If you just want to set the owner then the following code should work..

$acl = Get-Acl $FTPPath
$acl.SetOwner([System.Security.Principal.NTAccount] “subsun“)
Set-Acl $FTPPath $ACl

Open in new window

Avatar of CaussyR

ASKER

From your advise, I did the following :

$FTPPath = 'c:\temp\test'
$acl = get-acl $FTPPath
$acl.SetOwner([System.Security.Principal.NTAccount] "domain\Caussyr")
set-acl $FTPPath $acl

But the error I get now is,

set-acl : The security identifier is not allowed to be the owner of this object.
At line:4 char:1
+ set-acl $FTPPath $acl
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (C:\temp\test:String) [Set-Acl], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.SetAclCommand
ASKER CERTIFIED SOLUTION
Avatar of CaussyR
CaussyR

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CaussyR

ASKER

Through searching I found out that Microsoft have a simple module that can be imported for ACL.