Sometimes you may want to configure the inheritance on folders.
To be precise: The NTFS inheritance from parent folder(s).
For this, I wrote the following little function, I hope you find
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
function Set-NTFSInheritance { <# .SYNOPSIS Enable or Disable the NTFS permissions inheritance. .DESCRIPTION Enable or Disable the NTFS permissions inheritance on files and/or folders. .EXAMPLE $Folders = Get-Childitem -Path 'e:\homedirs' | Where-Object {$_.Attributes -eq 'Directory'} $Folders | foreach { $_ | Set-NTFSInheritance -Enable } .NOTES Author : Jeff Wouters Date : 8th of May 2014 #> [cmdletbinding(defaultparametersetname='Enable')] param ( [parameter(mandatory=$true,position=0,valuefrompipeline=$true,parametersetname='Enable')] [parameter(mandatory=$true,position=0,valuefrompipeline=$true,parametersetname='Disable')] $Path, [parameter(mandatory=$false,parametersetname='Enable')][switch]$Enable, [parameter(mandatory=$false,parametersetname='Disable')][switch]$Disable ) begin { } process { $ACL = get-acl $_.FullName switch ($PSCmdlet.ParameterSetName) { 'Enable' { $ACL.SetAccessRuleProtection($false,$false) } 'Disable' { $ACL.SetAccessRuleProtection($true,$true) } } try { $ACL | Set-Acl -Passthru } catch { $_.Exception } } end { } } |
it useful:
Okay, so how exactly do we run this?
PowerShell n00b
Hi,
That’s why I included an example in the help 🙂
$Folders = Get-Childitem -Path ‘e:\homedirs’ | Where-Object {$_.Attributes -eq ‘Directory’}
$Folders | foreach {
$_ | Set-NTFSInheritance -Enable
}
Jeff.
Try this, the one above is set to run from a powershell comandlet:
Param
(
[Parameter(Mandatory = $true, Position = 0)]
[Alias(‘Pa’)]
[string]$Path,
[Parameter(Mandatory = $true, Position = 0)]
[Alias(‘Sw’)]
$SetSwitch
)
Function Set-NTFSInheritance {
Begin {
Filter Set-Permissions {
$ACL = Get-Acl $_.FullName
Switch ($SetSwitch) {
‘Enable’ {
$ACL.SetAccessRuleProtection($false,$false)
}
‘Disable’ {
$ACL.SetAccessRuleProtection($true,$true)
}
default {}
}
Try {
$ACL | Set-Acl -Passthru
}
Catch {
$_.Exception
}
}
}
Process {
Try {
$Folders = Get-Childitem -Path $Path | Where-Object {$_.Attributes -eq ‘Directory’}
$Folders | foreach {
$_ | Set-Permissions $SetSwitch
}
}
Catch {
$_.Exception
}
}
End {}
}
Set-NTFSInheritance -Pa $Path -Sw $SetSwitch
Save it with this name Set-NTFSInheritance.ps1 and run it like this from powershell:
.Set-NTFSInheritance.ps1 “D:YourPath” Enable