title: powershell create folder and AD permission from array
body: <p>I am have an Active Directory OU that contains a large number of security groups (course sections that contain students and teachers usernames)
  I'd like to script via powershell that creates folders named using the security groups names then assign each security group the MODIFY permission to the corresponding folder.
  Here is my script</p>
  
  <pre><code>#get all security groups
  $sec_groups = Get-ADObject -SearchBase "ou=class,ou=Groups,ou=Colleague,dc=school,dc=edu" -LDAPFilter "(name=13_su*)" -ResultSetSize 10 | FT Name -HideTableHeaders 
  
  ForEach ($sec_group in $sec_groups)
  {
  #create folder for each security group
  $newPath = Join-Path -Path "\\server\classes" -childpath $sec_group
  New-Item $newPath -type directory
  
  #set permission for each folder
  $acl = Get-Acl $newpath
  $acl.SetAccessRuleProtection($True, $False)
  $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("labadmins","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
  $acl.AddAccessRule($rule)
  $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$sec_group","Modify", "ContainerInherit, ObjectInherit", "None", "Allow")
  $acl.AddAccessRule($rule)
  
  Set-Acl $newPath $acl
  }
  </code></pre>
  
  <p>It succeeds to create a folder for each security group but fails to set the <code>$sec_group</code> permission.</p>
  
  <p>What am I doing wrong?</p>