PowerShell to assign permissions to home directories
I have a situation where user data is migrating from one forest to another and there is a need to set permissions on folders which will form home ‘drives’ for users. The users’ home directory names match their SAM account names, I have written a simple PowerShell script to assign full control NTFS permissions to those folders based on the name of the user.
#script to give full control NTFS permissions on a directory to the domain user with the same name of that directory
#script settings
$domain = “robsdesk”
$root = “c:\data”
#don’t edit below here
$folders = Get-ChildItem $root
ForEach ($folder in $folders)
{
$username = $domain+“\”+$folder
$permissions = Get-Acl $folder
$userpermissions = New-Object System.Security.AccessControl.FileSystemAccessRule($username,“FullControl”, “ContainerInherit, ObjectInherit”, “None”, “Allow”)
$permissions.AddAccessRule($userpermissions)
Set-Acl $folder $permissions
Write-Host“Set permissions on $folder for $username”
}
The two lines to edit are the $domain and $root lines, the $domain line should be your domain name, this is used to construct the ‘domainname\user’ text used in the script. The $root variable is the root folder containing the folders to have permissions applied.
It’s quick and dirty – only used for a one off & there’s no error checking so be careful before you use it!
Rob