Creating a Home Drive with Windows PowerShell

With the following I will do 3 things:

1. Set the user his with drive letter
2. Create a on your home folders file share
3. Giving users full control on there folder.

 

The Script:

Get-ADUser -Filter * -SearchBase R0;OU=wardusers,DC=wardvissers,DC=local” | Foreach-Object {
$sam = $_.SamAccountName
$sid = $_.Sid
$HomeDrive=’J:’
$Domain=wardvissers.local’
$UserRoot=’\\wardvissers.local\dfs\home\’
$HomeDir=$UserRoot+$sam

# Assign the Drive letter and Home Drive for the user in

SET-ADUSER $sam –HomeDrive $HomeDrive –HomeDirectory $HomeDir

# Create the on the root of the Users Share

NEW-ITEM –path $HomeDir -type directory -force

$account=$Domain+’\’+$Accountname

# Set parameters for Access rule

$rights=[System..AccessControl.FileSystemRights]::FullControl
$inheritance=[System..AccessControl.InheritanceFlags]R1;ContainerInherit,ObjectInherit”
$propagation=[System..AccessControl.PropagationFlags]::None
$allowdeny=[System..AccessControl.AccessControlType]::Allow
$dirACE=New-Object System..AccessControl.FileSystemAccessRule ($sid,$rights,$inheritance,$propagation,$allowdeny)
$dirACL=Get-Acl $HomeDir

$dirACL.AddAccessRule($dirACE)

Set-Acl -path $HomeDir -AclObject $dirACL

Write-Host $HomeDir access assigned

}

Translate »
%d bloggers like this: