When setting the -homedirectory switch on a user through Powershell the directory is not created.
Use this code to create the folder and apply the necessary ACLs:
if ( !(Test-Path -Path "$homedir$userprincipalname" -PathType Container) ) {
## Doesn't exist so create it.
Write-Host "home directory doesn't exist. Creating home directory."
## Create the directory
New-Item -path $homedir -Name $userprincipalname -ItemType Directory
$userDir = "$homedir$userprincipalname"
## Modify Permissions on homedir
$Rights= [System.Security.AccessControl.FileSystemRights]::Read -bor [System.Security.AccessControl.FileSystemRights]::Write -bor [System.Security.AccessControl.FileSystemRights]::Modify -bor [System.Security.AccessControl.FileSystemRights]::FullControl
$Inherit=[System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit
$Propogation=[System.Security.AccessControl.PropagationFlags]::None
$Access=[System.Security.AccessControl.AccessControlType]::Allow
$AccessRule = new-object System.Security.AccessControl.FileSystemAccessRule("$userprincipalname",$Rights,$Inherit,$Propogation,$Access)
$ACL = Get-Acl $userDir
$ACL.AddAccessRule($AccessRule)
$Account = new-object system.security.principal.ntaccount($userprincipalname)
$ACL.setowner($Account)
$ACL.SetAccessRule($AccessRule)
Set-Acl $userDir $ACL
}
Thank you very much for this tip Shay Levy!
