Disabled Users Share Cleanup

Import-Module activedirectory

$localfolderlocation = "E:\Users\HomeDrives\"
$movedfilelocation = "F:\Account_Cleanup\Disabled_accounts\"

$userarray = Get-ADUser -Filter {Enabled -eq $false} | select -expandproperty samAccountName
$sortedusers = $userarray | Sort-Object
foreach ($username in $sortedusers)
$localfolderpath = "$localfolderlocation$username"
$newuserhomepath = "$movedfilelocation$username"
$userhomepath = Get-ADUser $username -properties homedirectory | select -expandproperty homedirectory

write-host -foregroundcolor Green "Looking up $username and share $userhomepath"
if(Test-Path $userhomepath)
echo "WARNING: $username is disabled but folder exists: $userhomepath" | Out-File -append ".\log.csv"
$colItems = (Get-ChildItem $userhomepath -recurse | Measure-Object -property length -sum)
$foldersize = "{0:N2}" -f ($colItems.sum / 1MB) + " MB"

echo "Copying $foldersize $userhomepath to $newuserhomepath" | Out-File -append ".\log.csv"
Copy-Item $userhomepath -destination $newuserhomepath -Recurse

#verify that the folder copied perfectly before proceeding
#Make sure new folder exists and then perform the cleanup on the old folder
if(Test-Path $newuserhomepath)
write-host -foregroundcolor Yellow "Folder successfully moved. Removing share."
Get-SmbShare -Name $username | Remove-SmbShare -Confirm:$false

echo "WARNING: Removing $localfolderpath"
Remove-Item -Path $localfolderpath -Force -Recurse
if(Test-Path $localfolderpath)
echo "ERROR: Folder was not properly removed: $localfolderpath"
if(-not(Test-Path $newuserhomepath))
echo "ERROR: Folder was not properly removed: $userhomepath"
elseif(-not(Test-Path $userhomepath))
echo "$username is disabled and share has already been moved." | Out-File -append ".\log.csv"