PowerShell function to configure inheritance on folders

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

it useful:

4 comments

  1. PowerShell N00b says:

    Okay, so how exactly do we run this?
    PowerShell n00b

  2. Jeff Wouters says:

    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.

  3. Ioan Popovici says:

    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

  4. Ioan Popovici says:

    Save it with this name Set-NTFSInheritance.ps1 and run it like this from powershell:
    .Set-NTFSInheritance.ps1 “D:YourPath” Enable

Leave a Reply

Your email address will not be published. Required fields are marked *