Wrong Security settings on Sub-folder

When I run the code below, I see that the parent folder has the right security, but the sub-folders have as security everyone and system (full control) and inheritance is off.
Make I an mistake in the code

 

$objPath = "${env:ProgramFiles(x86)}\Guentner"
Write-Log -Message "Set-Acl On folder $objPath" -Severity 1 -Source $deployAppScriptFriendlyName

#removes all inheritance for the folder
$acl = Get-Acl $objPath
$acl.SetAccessRuleProtection($true, $true)
$acl | Set-Acl $objPath

$objuser = New-Object System.Security.Principal.ntaccount("INGEBOUWD\Gebruikers")
$colRights = [System.Security.AccessControl.FileSystemRights]"CreateFiles, Appenddata, ReadAndExecute"
$inheritanceflag = [System.Security.AccessControl.InheritanceFlags]::None
$propagationflag = [System.Security.AccessControl.PropagationFlags]::None
$objtype = [System.Security.AccessControl.AccessControlType]::Allow
$objace = New-Object System.Security.AccessControl.FileSystemAccessRule($objuser, $colRights, $inheritanceflag, $propagationflag, $objtype)

$objacl = get-acl $objPath
$objacl.RemoveAccessRuleall($objace)
set-acl $objPath $objacl


$objuser = New-Object System.Security.Principal.ntaccount("D40\APPL_Guentner GPC")
$colRights = [System.Security.AccessControl.FileSystemRights]"FullControl"
$inheritanceflag = @([System.Security.AccessControl.InheritanceFlags]::ContainerInherit, [System.Security.AccessControl.InheritanceFlags]::ObjectInherit)
$propagationflag = [System.Security.AccessControl.PropagationFlags]::None
$objtype = [System.Security.AccessControl.AccessControlType]::Allow
$objace = New-Object System.Security.AccessControl.FileSystemAccessRule($objuser, $colRights, $inheritanceflag, $propagationflag, $objtype)

 

So you want the entire folder tree to be the same as the parent?
If this is the case, just as you would via the GUI, you have to tell it to do that, otherwise it will keep what it had.

Use the NTFSSecurity module from the MS PowerShellGallery.com for your use case.

 Find-Module -Name 'ntfs*' | ft -A

Version Name                    Repository Description                                                                                                     
------- ----                    ---------- -----------                                                                                                     
4.2.4   NTFSSecurity            PSGallery  Windows PowerShell Module for managing file and folder security on NTFS volumes                                 
1.0     NTFSPermissionMigration PSGallery  This module is used as a wrapper to the popular icacls utility to save permissions to a file and then restore...