Delete permissions on a folder.

Hello everyone,

 

I need to delete all user and group permissions from a folder. Unfortunately Powershell doesn't want to accept my wildcard * to delete everyone. Is there a way to delete everyone's permission from that folder?

What I have now:

 

$homedir="ipsum\ipsum2\martijn"

#Delete Inheritance (WORKS)
$acl = get-acl $homedir
$isProtected = $true 
$preserveInheritance = $true 
$acl.SetAccessRuleProtection($isProtected, $preserveInheritance) 
Set-Acl -Path $homedir -AclObject $acl 

#Delete the permissions (DOESN'T WORK)
$acl = get-acl $homedir
$account = new-object system.security.principal.ntaccount("*")
$acl.purgeaccessrules($account)
set-acl -aclobject $acl -path $homedir

 

As you can see I already deleted the Inheritance but still need to delete ALL of the permissions on the folder. This line gives an error:

$account = new-object system.security.principal.ntaccount("*")

Because it doesn't know what I mean with the wildcard *. So does anyone know how I can do this with only Powershell 2.0 and nothing else?

 

May 11th, 2011 4:25pm

#Delete Inheritance and Delete all permissions
$homedir = "ipsum\ipsum2\martijn
$acl = get-acl $homedir
$isProtected = $true 
$preserveInheritance = $false
$acl.SetAccessRuleProtection($isProtected, $preserveInheritance) 
Set-Acl -Path $homedir -AclObject $acl 
Free Windows Admin Tool Kit Click here and download it now
May 11th, 2011 4:32pm

Thank you Kazun for the really fast reply!

This works when all of the folders have inheritance, but this is not the case on my side.

I need to do this to make homedirs for users. I want to delete all of the permissions and then add that user to his/her homedir.

May 11th, 2011 4:41pm

Thank you Kazun for the really fast reply!

This works when all of the folders have inheritance, but this is not the case on my side.

I need to do this to make homedirs for users. I want to delete all of the permissions and then add that user to his/her homedir.

$acl.Access | % {$acl.purgeaccessrules($_.IdentityReference)}

PS.  Inheritance flag uncheck.


Free Windows Admin Tool Kit Click here and download it now
May 11th, 2011 4:48pm

It worked! Thank you very very much!

I have one extra question and really hoped u can help me with this as you just did with that delete question.

As I said I wanted to delete all of them and add an account to that file but because everyone has been deleted it doesn't have any permission to add permissions to it. I tried this with your help and although this may seem very stupid I hoped it worked which it doesn't.

# Remove and add permissions
$homedir="ipsum\ipsum2\martijn"

$acl.Access | % {$acl.purgeaccessrules($_.IdentityReference)}

$account="oxl\martijn"
$rights=[System.Security.AccessControl.FileSystemRights]::FullControl
$inheritance=[System.Security.AccessControl.InheritanceFlags]"ContainerInherit,ObjectInherit"
$propagation=[System.Security.AccessControl.PropagationFlags]::None
$allowdeny=[System.Security.AccessControl.AccessControlType]::Allow

$dirACE=New-Object System.Security.AccessControl.FileSystemAccessRule ($account,$rights,$inheritance,$propagation,$allowdeny)
$dirACL=Get-Acl $homedir
$dirACL.AddAccessRule($dirACE)
Set-Acl -aclobject $homedir $dirACL
Write-Host $homedir Permissions added

This is the error:

Set-Acl : Cannot bind parameter 'AclObject'. Cannot convert the "ipsum\ipsum2\martijn" value of type "System.String" to type "System.Security.AccessControl.ObjectSecurity"
.

 

May 11th, 2011 5:06pm

$homedir = "ipsum\ipsum2\martijn"
$acl = Get-Acl $homedir
$acl.Access | % {$acl.purgeaccessrules($_.IdentityReference)}

$account="oxl\martijn"
$rights=[System.Security.AccessControl.FileSystemRights]::FullControl
$inheritance=[System.Security.AccessControl.InheritanceFlags]"ContainerInherit,ObjectInherit"
$propagation=[System.Security.AccessControl.PropagationFlags]::None
$allowdeny=[System.Security.AccessControl.AccessControlType]::Allow

$dirACE=New-Object System.Security.AccessControl.FileSystemAccessRule ($account,$rights,$inheritance,$propagation,$allowdeny)
$ACL.AddAccessRule($dirACE)

Set-Acl -aclobject $ACL -Path $homedir
Write-Host $homedir Permissions added



Free Windows Admin Tool Kit Click here and download it now
May 11th, 2011 5:12pm

It doesn't give an error now, but it only adds the account martijn and doesn't delete the other accounts.

Code:

# Remove and add permissions
$homedir="ipsum\ipsum2\martijn"

$acl.Access | % {$acl.purgeaccessrules($_.IdentityReference)}

$account="oxl\martijn"
$rights=[System.Security.AccessControl.FileSystemRights]::FullControl
$inheritance=[System.Security.AccessControl.InheritanceFlags]"ContainerInherit,ObjectInherit"
$propagation=[System.Security.AccessControl.PropagationFlags]::None
$allowdeny=[System.Security.AccessControl.AccessControlType]::Allow

$dirACE=New-Object System.Security.AccessControl.FileSystemAccessRule ($account,$rights,$inheritance,$propagation,$allowdeny)
$dirACL=Get-Acl $homedir
$dirACL.AddAccessRule($dirACE)
Set-Acl -aclobject $dirACL -Path $homedir
Write-Host $homedir Permissions added
What a stupid mistake from me with setting the Acl.

May 11th, 2011 5:24pm

$homedir = "ipsum\ipsum2\martijn"
$acl = Get-Acl $homedir
if ($acl.AreAccessRulesProtected) { $acl.Access | % {$acl.purgeaccessrules($_.IdentityReference)} }
else {
		$isProtected = $true 
		$preserveInheritance = $false
		$acl.SetAccessRuleProtection($isProtected, $preserveInheritance) 
	 }

$account="oxl\martijn"
$rights=[System.Security.AccessControl.FileSystemRights]::FullControl
$inheritance=[System.Security.AccessControl.InheritanceFlags]"ContainerInherit,ObjectInherit"
$propagation=[System.Security.AccessControl.PropagationFlags]::None
$allowdeny=[System.Security.AccessControl.AccessControlType]::Allow

$dirACE=New-Object System.Security.AccessControl.FileSystemAccessRule ($account,$rights,$inheritance,$propagation,$allowdeny)
$ACL.AddAccessRule($dirACE)

Set-Acl -aclobject $ACL -Path $homedir
Write-Host $homedir Permissions added

Free Windows Admin Tool Kit Click here and download it now
May 11th, 2011 5:28pm

IT WORKED!

I found out that $CL needed to be $ACL which you have edited. Thank you so much! You really made my day. Great to see a forum like this with a lot of active and very helpful people that are willing to help beginners like me.

Again, thank you!

May 11th, 2011 5:40pm

Just want you to know...2yrs later....this helped me solve a somewhat similar problem. Thanks much! It was the final piece to my somewhat difficult (for me, at least) puzzle! :)

Regards.
Shane (@coolsport00)

Free Windows Admin Tool Kit Click here and download it now
April 17th, 2013 9:18pm

I'm happy to hear that it helped you out. Good luck with your scripting!
April 18th, 2013 1:33am

Very helpful, thank you!
Free Windows Admin Tool Kit Click here and download it now
August 6th, 2013 8:02am

Tried this script and it worked just fine, but, when I create something in the folder, files or directories they don't get the right permissions, what can be wrong?
August 18th, 2013 5:55am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics