08 May 2014 @ 12:38 PM 

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:

Posted By: Jeff Wouters
Last Edit: 08 May 2014 @ 12:38 PM

EmailPermalink
Tags
Categories: DuPSUG, PowerShell


  • PowerShell N00b

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

  • http://www.jeffwouters.nl 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


 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 346
  • Comments » 503
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

About



    No Child Pages.

Contact



    No Child Pages.

Speaking



    No Child Pages.

Health Check



    No Child Pages.