try {
$BackupDate = Get-Date -format "yyyyMMddhhmmss"
$Profiles = Get-CimInstance -ClassName Win32_UserProfile -Filter "Special != 'True'"
Write-Host ""
Write-Host "Please choose:"
Write-Host ""
$menu = @{}
$i = 0
foreach ($Profile In $Profiles) {
Write-Host "`t$($i) - $($Profile.LocalPath)"
$menu.Add($i, $Profile)
$i++
}
Write-Host ""
[int]$Option = Read-Host "Type the number corresponding to the profile to backup and delete"
$UserProfile = $menu.Item($Option)
if ($UserProfile) {
if ($UserProfile.Loaded -eq $false) {
# Rename File System Folder
if ((Test-Path -Path $UserProfile.LocalPath) -eq $true) {
$BackupFolderName = "$($UserProfile.LocalPath).$($BackupDate).bak"
Rename-Item -Path $UserProfile.LocalPath -NewName $BackupFolderName
if ((Test-Path -Path $BackupFolderName) -eq $true) {
Write-Host "File system profile folder renamed."
}
else {
Throw "File system profile folder not renamed."
}
}
else {
Write-Host "File system profile folder does not exist."
}
# Remove profile
try {
$UserProfile | Remove-CimInstance
Write-Host "User profile deleted."
}
catch {
Throw $_
}
}
else {
Throw "User profile is currently loaded and cannot be modified."
}
}
else {
Write-Host "User profile not found."
}
}
catch {
Throw $_
}