Powershell Set Permissions on home directory
<p>I am trying to use powershell to create home directories for users as well as set permissions for them accordingly.</p>

<p>I appear to keep running into this issue where I am unable to set the permissions. I am getting this error:</p>

<blockquote>
  <p>Exception calling "AddAccessRule" with "1" argument(s): "Some or all identity references could not be translated."
  At line:1 char:1
  + $permissions.AddAccessRule($userpermissions)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : IdentityNotMappedException</p>
</blockquote>

<p>Here is the section of code:</p>

<pre><code>$UserDir = [string]::Format("{0}\{1}", $HomeDirRoot, $user["UserName"]) 
$Account = [string]::Format("{0}\{1}", "Domain", $user["UserName"])

#Create Directory
New-Item -ItemType directory -Path $UserDir |Out-Null

# assign file permissions 

$FileSystemAccessRights=[System.Security.AccessControl.FileSystemRights]"FullControl"
$InheritanceFlags=[System.Security.AccessControl.InheritanceFlags]::"ContainerInherit", "ObjectInherit"
$PropagationFlags=[System.Security.AccessControl.PropagationFlags]::None
$AccessControl=[System.Security.AccessControl.AccessControlType]::Allow

$NewAccessrule = New-Object System.Security.AccessControl.FileSystemAccessRule ` ($Account, $FileSystemAccessRights, $InheritanceFlags, $PropagationFlags, $AccessControl) 
$CurrentACL=Get-ACL -path $UserDir
$CurrentACL.SetAccessRule($NewAccessrule)
Set-ACL -path $UserDir -AclObject $CurrentACL
</code></pre>

<p>I have tried several variations of this code and still end up with the same error. I have even got as far as simply getting the ACL object and then using it to set the permissions like below:</p>

<pre><code>$folder = "\\fs\home\SAMACCOUNTNAMEHERE"
$permissions = Get-Acl $folder
Set-Acl $folder $permissions
</code></pre>

<p>I am at a loss here. Because the code works on folders on my machine but when i try to run it on a folder like above it blows up with the error relating to identity references.</p>

<p>Any suggestions appreciated.</p>