PoshCode Archive  Artifact [2c9bbfbc3f]

Artifact 2c9bbfbc3f502964849f5c963a4f851d892f602eb52594165e440775109f829b:

  • File Folder-inheritance.ps1 — part of check-in [af52851124] at 2018-06-10 13:23:34 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: 3299
# x-archived: 2016-08-05T07:04:29
# x-published: 2012-03-28T19:39: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