Profile Cleaning: User Logoff & Delete

2 min read
Profile Cleaning: User Logoff & Delete
When its time to say good bye. A quick way to logoff and delete a user profile.

Stale Profile

At times I’ve heard people say a user profile is messed up, somethings wrong. This is much more complicated today with .vhd’s, roaming profiles, fslogix, things like Ivanti hooks and more. How times have changed.

For today we’ll take a look at a simpler time when a users profile and all their settings is on a given computer and how we can clean that off.

Below is the powershell script I like to call: User Logoff & Profile Delete. When ran you’ll be prompted to enter a computer name and user name. The command will run to logoff that user from that given computer and then after delete the user profile. I’ve provided this to the service desk and desktop support teams as another tool in their chest to help user when trouble shooting live calls.

Best Regards,
Michael Wood

#---------------Display Banner--------------------

Write-Host ""
Write-Host "╒═══════════════════════ " -NoNewLine
Write-Host "USER LOGOFF & PROFILE DELETE " -Foreground Cyan -NoNewLine
Write-Host "════════════════════════╕"
Write-Host "│                                                                             │"
Write-Host "│ This application will first attempt to logoff a user from a computer then   │"
Write-Host "│ delete the user local C:\Users profile folder and registry entry.           │"
Write-Host "│                                                                             │"
Write-Host "│ You will be asked for the following:                                        │"
Write-Host "│ 1. Computer Name                                                            │"
Write-Host "│ 2. User Name (Active Directory Account)                                     │"
Write-Host "│                                                                             │"
Write-Host "│" -NoNewLine
Write-Host " NOTE: You must have permission to delete user profiles.                    " -Foreground Red -NoNewLine
Write-Host " │"
Write-Host "│                                                                             │"
Write-Host "╘═════════════════════════════════════════════════════════════════════════════╛"
Write-Host ""

#---------------Variables--------------------

$ComputerName = Read-Host ' Enter Computer Name'
$UserName = Read-Host ' Enter AD Account'
$StartTime = Get-Date
$Session = ((quser /server:$ComputerName | ? { $_ -match $UserName }) -split ' +')[2]


#---------------Display Banner Continued--------------------

CLS
Write-Host ""
Write-Host " ═════════════════════════════════ " -NoNewLine
Write-Host "RESULTS " -Foreground Cyan -NoNewLine
Write-Host "════════════════════════════════════"
Get-Date
Write-Host ""
Write-Host "Processing the user logoff and local profile deletion requested" -Foreground Yellow
Write-Host ""
Write-Host "Computer: $ComputerName"
Write-Host "    User: $UserName"


#---------------Application--------------------

LogOff $Session /server:$ComputerName
# Get-WmiObject -Class Win32_UserProfile –ComputerName $ComputerName |  Where-Object { $_.LocalPath -eq "C:\Users\$UserName" } | foreach {$_.Delete()}
Get-CimInstance -ComputerName $ComputerName -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq $UserName } | Remove-CimInstance

#---------------Good Bye Message---------------

$FinishTime = Get-Date
CLS
Write-Host ""
Write-Host "════════════════════ " -NoNewLine
Write-Host "DELETE USER PROFILE ON REMOTE MACHINE " -Foreground Cyan -NoNewLine
Write-Host "════════════════════"
Write-Host ""
Write-Host -ForegroundColor Green "Completed!"
Write-Host "Deleted the following user profile: $UserName on computer $ComputerName"
Write-Host ""
Write-Host -ForegroundColor Cyan "Start Time:"
Write-Host "$StartTime"
Write-Host ""
Write-Host -ForegroundColor Cyan "Finish Time:"
Write-Host "$FinishTime"
Write-Host ""
Read-Host "Press any key to exit application"
Virtualize Brief


Follow