waar is de beste plaats om te kopen van voorschrift brillen http://hollandpillen.com/lorazepam/ beste otc koud geneeskunde
Jeff Wouters's Blog

PowerShell function to configure inheritance on folders

by on May.08, 2014, under DuPSUG, PowerShell

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

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:


4 Comments for this entry

  • PowerShell N00b

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

  • Jeff Wouters

    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.

  • Ioan Popovici

    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

  • Ioan Popovici

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

Leave a Reply