Create File Folder and permission

I needed a quick script to include in my runbooks – one to create the home drive for my new user process. I got the base for this off the internet ( https://activedirectoryfaq.com/2017/09/powershell-create-home-directory-grant-permissions/ ) but I wanted to add a little bit of checking to see if the file existed first, etc.

param([Parameter(Mandatory=$true)][String]$samAccountName)

$User = Get-ADUser -Identity $samAccountName -Properties l

$office = $User.l

$homePath = "\\my.netork\user$\" + $Office + "\{0}" -f $samAccountName

 
if($User -ne $Null) {
	
	#check home path to make sure one doesn't already exist
	[string]$homepathtest = Test-Path $homePath
	
	If ($homepathtest -eq "False") 
		{
		#create home drive
	    $homeShare = New-Item -path $homePath -ItemType Directory -force -ea Stop
	 	
	    $acl = Get-Acl $homeShare
	 	
		#permissison home drive
	    $FileSystemRights = [System.Security.AccessControl.FileSystemRights]"Full"
	    $AccessControlType = [System.Security.AccessControl.AccessControlType]::Allow
	    $InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
	    $PropagationFlags = [System.Security.AccessControl.PropagationFlags]"InheritOnly"
	 
	    $AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule ($User.SID, $FileSystemRights, $InheritanceFlags, $PropagationFlags, $AccessControlType)
	    $acl.AddAccessRule($AccessRule)
	 
	    Set-Acl -Path $homeShare -AclObject $acl -ea Stop
	 	
	    Write-Host ("HomeDirectory created at {0}" -f $homePath)
	
		}
		
	If($homepathtest -eq "true") {
		Write-Host ("Home Directory at {0} already exists" -f $homePath )
		}
	
#
}