Delete-UnusedHomeFolders.ps1
something exciting
Some information about the exciting thing
Table of contents generated with markdown-toc
Script
#This should point to the root of a home folder/FR share. Subfolders in this folder match AD account usernames.
$Folders = Get-ChildItem \\SERVER\SHARE\FOLDER\
$NamesToIgnore = "Username"
$FoldersToBeRemoved = @()
foreach ($Folder in $Folders) {
$Username = $Folder.Name
if ($NamesToIgnore -notcontains $Username) {
$UsernameCheck = Get-ADUser -Identity $Username -Properties * -ErrorAction SilentlyContinue
if ($? -eq $false -and $Username -notlike $NamesToIgnore) {
$FoldersInformation = @{"Folder Name" = $Username; "Folder Path" = $Folder.FullName; "Last Write Time" = $Folder.LastWriteTime }
$FoldersToBeRemoved += New-Object -TypeName psobject -Property $FoldersInformation
Write-Host $Username
pause
Remove-Item $Folder.FullName -Recurse -Force -Verbose -WhatIf
}
}
}
$FoldersToBeRemoved | Out-GridView
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simple click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.