What you are looking for is Get-ACL
and Set-ACL
. You can manually setup one folder as you want it to be setup, then run Get-ACL
against that to pull all the security info from it. Then you can apply that to the new folders as they are created with Set-ACL
. So something like:
[int]$YY = Get-Date -f "yy"
[String]$YY = "$($YY-1)-$YY"
$ACL = Get-ACL C:\Test\Template
$ACLSub = Get-ACL "C:\Test\Template\Sub"
"AD","CC","DC","FS","IT","OP" | %{
$CurrentRoot = New-Item "C:\Test\$_" -ItemType Directory
$CurrentSub = New-Item "$CurrentRoot\$YY" -ItemType Directory
$CurrentRoot | Set-ACL -AclObject $ACL
$CurrentSub | Set-ACL -AclObject $ACLSub
}
Then you just need to setup the Template folder and the Sub folder within it to have the correct security permissions ahead of time and you are all set. It's much simpler than setting all the permissions by hand in PowerShell IMHO.
Edit: Well, considering that you have a folder list in a text file this actually becomes simpler.
$Folders = Get-Content "C:\Users\david\Desktop\PowershellFoldersDB.txt"
[int]$YY = Get-Date -f "yy"
[String]$YY = "$($YY-1)-$YY"
$ACL = Get-ACL C:\Test\Template
$ACLSub = Get-ACL "C:\Test\Template\Sub"
$folders |?{$_ -match "^(C:\\Test\\..)(\\\d{2}-\d{2})(\\.*)$"}|%{New-Item "$($Matches[1])\$YY$($Matches[3])" -ItemType directory -force | Out-Null}
GCI "$($matches[1])\.." -Directory | %{
$_ | Set-ACL -AclObject $ACL
GCI $_.FullName | Set-ACL -AclObject $ACLSub
}