There are a few options available when it comes to cleaning up old and obsolete user profiles on a remote computer. You can turn on this Group Policy that automatically deletes any user profiles older than a certain period of days on system restart, or use a command-line tool like Delprof2. And of course, you can also use PowerShell to accomplish the same as well.
To list all user profiles on a local computer, we can simply use Get-WmiObject with Win32_UserProfile class.
Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.Special -ne 'Special'} | Select-Object LocalPath, Loaded
Add a -Computer switch and you will get a list of user profiles on a remote computer.
Get-WmiObject -Class Win32_UserProfile -Computer $computer | Where-Object {$_.Special -ne 'Special'} | Select-Object LocalPath, Loaded
Note that you can’t delete any loaded user profiles. You will need to either log off that user or restart the computer before removing their user them.
To remove a specific user profile, simply use the cmdlet Remove-WmiObject. But the problem is that you can’t pipe a result out to it to carry out the deleting process.
The way around is to use Invoke-Command to execute the command right on the remote computer. Putting all together,
$Computer = Read-Host "Please Enter Computer Name: "
$user = Read-Host "Enter User ID: "
Invoke-Command -ComputerName $computer -ScriptBlock {
param($user)
$localpath = 'c:\users\' + $user
Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq $localpath} |
Remove-WmiObject
} -ArgumentList $user
The beauty of using PowerShell to do this is that you remove a particular user across multiple computers. You can also run this as part of your employee-departure routine, among many other things.
Lastly, I thought to mention that in order to get the PowerShell scripts to work on remote computers there are two prerequisites that need to be met.
- WinRM needs to be enabled on the remote computer
- You need proper credentials to run the script on the remote computer.
Legend!
Works great! Is there anyway to implement a function to delete the corresponing folder, e.g. c:\users\username?
most of the time, it deletes that folder automatically.
Can I use this script with this:
$computers = Get-Content .\comps.txt | Get-ADComputer -ErrorAction Continue
So can deleta all users on multipla computers at once?
When I run the above command I am getting error
Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings.
At line:1 char:1
+ Invoke-Command -ComputerName $ “Compuer-name” -ScriptBlock {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException
+ FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand
Awesome. thank you so much.
$user = Read-Host “Enter User Name”
$computers = Get-Content “D:\Temp\computers.txt”
Write-Host(“”)
ForEach ($Computer in $Computers)
{
Write-Host “Processing $Computer” -NoNewLine
Try {
Invoke-Command -ComputerName $computer -ScriptBlock {
param($user)
$localpath = ‘c:\users\’ + $user
Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq $localpath} |
Remove-WmiObject
} -ArgumentList $user -ErrorAction Stop
Write-Host “`t…Completed”
}
Catch
{
Write-Host “`t…Connection timed out”
}
}
Removing a user profile manually (or via script) without removing it also from the registry can cause issues.