PowerShell Creating a Home Folder for Users

Hi Readers,

Sharing a script that I have written recently for Creatiing Home folder, adding permissions & updating Active directory home folder path.

Download the zip file from below link & extract the script files.

https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Creating-a-Home-c20fe6cb

Update the users.txt file with user logon names/samaccountnames

Update the variables in the script & run the batch file to execute.

###################Define Variables####
$NetPath = “\\labNas\users$”

$users = get-content .\users.txt

$dletter = “H:”
#######################################

This script will create folder names at the location provided, & connect as H: drive in active directory

if  fields are already populated than script will throw warnings that value/folder already exists.

Pls rate the contribution if it worked for you.

download Quest Management Shell (its a free shell) –> http://www.quest.com/powershell/activeroles-server.aspx

######################################################################### 
#        Author: Vikas SUkhija 
#               Description: Create Home folder 
#               date: 09/17/2014 
#                
######################################################################### 
 
$date = get-date -format d 
# replace \ by - 
$time = get-date -format t 
$month = get-date  
$month1 = $month.month 
$year1 = $month.year 
 
$date = $date.ToString().Replace(“/”, “-”) 
 
$time = $time.ToString().Replace(":""-"$time = $time.ToString().Replace(" """) 
 
 
$logs = ".\" + "Powershell" + $date + "_" + $time + "_.txt" 
 
start-transcript $logs 
 
# ListDirectory, ReadData, WriteData  
# CreateFiles, CreateDirectories, AppendData  
# ReadExtendedAttributes, WriteExtendedAttributes, Traverse 
# ExecuteFile, DeleteSubdirectoriesAndFiles, ReadAttributes  
# WriteAttributes, Write, Delete  
# ReadPermissions, Read, ReadAndExecute  
# Modify, ChangePermissions, TakeOwnership 
# Synchronize, FullControl 
 
If ((Get-PSSnapin | where {$_.Name -match "Quest.ActiveRoles.ADManagement"}) -eq $null) 
{ 
    Add-PSSnapin Quest.ActiveRoles.ADManagement 
} 
 
###################Define Variables#### 
 
$NetPath = "\\labnas\users$" 
$users = get-content .\users.txt 
$dletter = "H:" 
 
####################################### 
 
$users | foreach-object{ 
 
$qaduser = get-qaduser $_ 
 
$userhomepath = $NetPath + "\" + $_ 
 
    if(-not(Test-Path $userhomepath)) 
    { 
 
    New-Item -Path $userhomepath -ItemType Directory 
        Write-host "$userhomepath ------Created" -foregroundcolor Blue 
        $acl = get-acl $userhomepath 
 
    $inheritanceFlags = ([Security.AccessControl.InheritanceFlags]::ContainerInherit -bor ` 
                         [Security.AccessControl.InheritanceFlags]::ObjectInherit) 
    $propagationFlags = [Security.AccessControl.PropagationFlags]::None 
 
 
        $permissions = $_,"Modify",$inheritanceFlags,$propagationFlags,"Allow" 
        $access = New-Object  system.security.accesscontrol.filesystemaccessrule($permissions) 
    $acl.SetAccessRule($access) 
    $acl | Set-Acl $userhomepath  
        $homedir = $qaduser.HomeDirectory 
            if ($homedir -like $null) 
                   { 
                     
                   Get-QADUser $_ | Set-QADUser -ObjectAttributes @{HomeDirectory = $userhomepath} 
                   Get-QADUser $_ | Set-QADUser -ObjectAttributes @{HomeDrive = $dletter} 
                   $usr = Get-QADUser $_ 
 
                   Write-host "Added Homedrive "$usr.HomeDrive" and Home directory "$usr.HomeDirectory""  -foregroundcolor Green 
                   } 
                 else {Write-host "$homedir already exists in AD for $_" -foregroundcolor yellow} 
                  
        } 
        else 
        { 
 
    Write-Warning -Message "'$userhomepath' already exists." 
        $homedir = $qaduser.HomeDirectory 
                if ($qaduser.HomeDirectory -like $null) 
                   { 
                     
                   Get-QADUser $_ | Set-QADUser -ObjectAttributes @{HomeDirectory = $userhomepath} 
                   Get-QADUser $_ | Set-QADUser -ObjectAttributes @{HomeDrive = $dletter} 
                   $usr = Get-QADUser $_ 
 
                   Write-host "Added Homedrive "$usr.HomeDrive" and Home directory "$usr.HomeDirectory""  -foregroundcolor Green 
                   } 
                 else {Write-host "$homedir already exists in AD for $_" -foregroundcolor yellow} 
                  
 
       } 
} 
 
 
Stop-transcript 
########################################################################
 Regards
Sukhija Vikas
Advertisements

2 thoughts on “PowerShell Creating a Home Folder for Users

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s