Here is a group of Powershell scripts I wrote for account maintenance utilizing Dell Quick Connect
List accounts older than 2 years to remove HomeDirectory
#two years
$DaysInactive = 730
$lastLogonTimestamp= $srcObj["lastLogonTimestamp"]
$pwdLastSet = $srcObj["pwdLastSet"]
$response = $FALSE
$LastLoginResponse = $FALSE
$pwdLastSetResponse = $FALSE
if($lastLogonTimestamp){
$LastLogonConverted = [datetime]::FromFileTime([int64]::Parse($lastLogonTimestamp))
if( ((get-date) - $LastLogonConverted ).days -ge $DaysInactive ){
$LastLoginResponse = $TRUE
}
}
if($pwdLastSet){
$pwdLastSetConverted = [datetime]::FromFileTime([int64]::Parse($pwdLastSet))
if( ((get-date) - $pwdLastSetConverted ).days -ge $DaysInactive ){
$pwdLastSetResponse = $TRUE
}
}
if($LastLoginResponse -or $pwLastSetResponse){
$response = $TRUE
}
$response
Based on the OU determine HomeDirectory Location
$ParentPath = $dstObj["distinguishedName"]
$Path = ""
if($ParentPath){
if($ParentPath.Contains("Admins") -or $ParentPath.Contains("Staff") ){
$Path = "\\fs-c108-01\staff_home$\"
}elseif($ParentPath.Contains("Faculty")){
$Path = "\\fs-c108-04\faculty_home$\"
}elseif($ParentPath.Contains("Students") -or $ParentPath.Contains("Seminar")){
$Path = "\\fs-c108-03\student_home$\"
}else{
Write-Error "Cannot Find Where to Put Home Directory"
}
$Path += $dstObj["sAMAccountName"]
}else{
Write-Error "ParentPath Null"
}
$Path
Create HomeDirectory and Assign Permissions
$ParentPath = $srcObj["distinguishedName"]
$User = $srcObj["sAMAccountName"]
$DomainUser = "fitsuny\"
$DomainUser += $User
$Path = ""
if($ParentPath){
if($ParentPath.Contains("Admins") -or $ParentPath.Contains("Staff") ){
$Path = "\\fs-c108-01\staff_home$\"
}elseif($ParentPath.Contains("Faculty")){
$Path = "\\fs-c108-04\faculty_home$\"
}elseif($ParentPath.Contains("Students") -or $ParentPath.Contains("Seminar")){
$Path = "\\fs-c108-03\student_home$\"
}else{
Write-Error "Cannot Find Where to Put Home Directory"
}
$Path += $srcObj["sAMAccountName"]
}else{
Write-Error "ParentPath Null"
}
$HasDir = Test-Path $Path
if($HasDir){
Write-Error "Directory Exists"
}else{
New-Item $Path -type directory
$acl = Get-Acl $Path
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($DomainUser,"FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
Set-Acl $Path $acl
}
Remove HomeDirectory
$Path = $dstObj["Path"]
$Folder = $dstObj["Folder"]
$FullPath = $Path
$FullPath += $Folder
#Not Stable
#Remove-Item -Recurse -Force $FullPath
cmd /c rd /s /q $FullPath
$HasDir = Test-Path $FullPath
if($HasDir){
Throw "Unable to delete home directory"
}