Deleting a User's Local Profile with PowerShell

2015-09-19T17:00:00Z

In some situations it can be useful to remove a users local profile from a PC, for example, when testing installation of software into the users profile or even when testing the application of a Group Policy. This PowerShell one liner can accomplish just that:

(Get-WmiObject -Class win32_userprofile -ComputerName <RemoteComputerName> | `
    where-object { $_.localpath -like "*<UserName>" } ).delete()

Just substitute "<RemoteComputerName>" and "<UserName>" with appropriate values.

You may want to create a copy of the user profile first. In fact, copying a users profile, deleting it and then restoring any files that user needs is a useful way to remove some virus manifestations and/or deal with a corrupt local profile. To copy the users profile, issue the command:

Copy-Item -Path '\\<RemoteComputerName>\C$\Users\<UserName>' -Destination "D:\Temp" -Recurse

In this instance I've copied the user profile to D:\Temp. When the user logs back into the remote computer, their profile will be re-created. I can then restore desktop shortuts, documents and even registry entries from the copy of the profile that I've just taken. Of course, it's useful to log the user off first as to ensure their profile is captured in a consistent stated. You can action this with:

(Get-WmiObject win32_operatingsystem -ComputerName <RemoteComputerName>).Win32Shutdown(4)