In the previous tip we illustrated how Get-FolderStructureWithPermission can document and create a list of all nested folders in a structure, along with their respective NTFS security settings. The result could be saved in a variable, or serialized to disk using Export-Csv
Today we show you the second part: once you have the information about a given folder structure, here is Set-FolderStructureWithPermission. It accepts a path to a folder in which you would like to clone the folder structure, plus the structure information itself that you got from Get-FolderStructureWithPermission:
#requires -RunAsAdministrator function Set-FolderStructureWithPermission { param ( [String] [Parameter(Mandatory)] $Path, [Object[]] $folderInfo ) $folderInfo | ForEach-Object { $relativePath = $_.Path $sddl = $_.SDDL $newPath = Join-Path -Path $Path -ChildPath $relativePath $exists = Test-Path -Path $newPath if ($exists -eq $false) { $null=New-Item -Path $newPath -ItemType Directory -Force } $sd = Get-Acl -Path $newPath $sd.SetSecurityDescriptorSddlForm($sddl) Set-Acl -Path $newPath -AclObject $sd } }
Because of how NTFS permissions are set, this function requires Administrator privileges to run.
Disclaimer: all code presented here is just for information purpose. While we do take great care to test it, there is no warranty whatsoever, and it is no production-ready code. It is your responsibility to test and determine whether this code runs flawlessly and fits your need.
A typical use case could be the cloning of an existing folder structure:
# clone user profile $infos = Get-FolderStructureWithPermission -Path $home Set-FolderStructureWithPermission -Path c:CloneHere -folderInfo $infos