PoshCode Archive  Artifact [5044eec4ef]

Artifact 5044eec4efb5e67aa0832835fddceb9927b912211b59b3bc20e3a43bd440d23c:

  • File Folder-inheritance.ps1 — part of check-in [9adacce70a] at 2018-06-10 14:07:09 on branch trunk — Change the inheritance from parent with PowerShell (user: Vern Anderson size: 1356)

# encoding: ascii
# api: powershell
# title: Folder inheritance
# description: Change the inheritance from parent with PowerShell
# version: 0.1
# type: script
# author: Vern Anderson
# license: CC0
# x-poshcode-id: 6058
# x-archived: 2016-05-17T14:20:14
# x-published: 2016-10-21T14:09:00
#
#
# setup the test folders
   md c:\grandfather\father\son
   md c:\grandmother\mother\daughter

# By default the folders will inherit ACLs from the C: drive
# To toggle it or change it to the desired setting

# Will force the inheritance from parent
      $inheritance = get-acl c:\grandmother
      $inheritance.SetAccessRuleProtection($false,$false)
      set-acl c:\grandmother -aclobject $inheritance
#Display the new state
      (Get-Acl c:\grandmother).AreAccessRulesProtected

#############################################################
# Second script to go the other way
#############################################################

# Will remove the inheritance from parent

      $inheritance = get-acl c:\grandfather
      $inheritance.SetAccessRuleProtection($true,$true)
      set-acl c:\grandfather -aclobject $inheritance
#Display the new state
      (Get-Acl c:\grandfather).AreAccessRulesProtected

# As you can see changing the ($false,$false) to ($true,$true) is the only difference in the 2 scripts