It’s not that often, but every once and while there is a need to delete user profiles. It could be reasons like corrupt user profile, you just need to do some automated testing or just clean up a computer. So now we will learn how to programmatically delete user profiles with a PowerShell script. If you have knowledge in any other scripting language it should be rather easy to create a VBScript with the information provided here.
First we take a look in WBEMTEST, the built-in GUI to browse WMI.
Run WBEMTEST as administrator and press Connect…
When the popup appears just press Connect to connect to the local machine and the default Namespace.
Click Enum Classes… and choose Recursive and press OK
Wait until all classes have been enumerated and look for Win32_UserProfile and double click it, when the information for this class appears just click Instances to view the current instances for this class, or in this case all user profiles that logged on to this computer.
So here are all 7 users that logged on to my computer, this will include service account, system account etc.
(Known SID are documented here: http://support.microsoft.com/kb/243330)
One way to find out what SID is what user just double click the SID and look for the property LocalPath.
To delete a user profile just select the correct SID and press Delete and wait. This will delete the user profile folder and clean up registry, and could take a while.
OK, know we know how to do this manually via WMI! Is there any way we can do this with PowerShell script? Yes!
So what do we know, the information is located in the class Win32_UserProfile and we can use the LocalPath to filter the correct user profile.
First we try the Get-WmiObject command to list all of our User Profiles, if you compare the results it will be the same as in WBEMTEST
>Get-WmiObject -Class Win32_UserProfile __GENUS : 2 __CLASS : Win32_UserProfile __SUPERCLASS : __DYNASTY : Win32_UserProfile __RELPATH : Win32_UserProfile.SID="S-1-5-21-2810523171-724750113-4076201095-500" __PROPERTY_COUNT : 12 __DERIVATION : {} __SERVER : W7-2 __NAMESPACE : root\cimv2 __PATH : \\W7-2\root\cimv2:Win32_UserProfile.SID="S-1-5-21-2810523171-724750113-4076201095-500" LastDownloadTime : LastUploadTime : LastUseTime : 20121119105717.856000+000 Loaded : False LocalPath : C:\Users\Administrator RefCount : 0 RoamingConfigured : False RoamingPath : RoamingPreference : SID : S-1-5-21-2810523171-724750113-4076201095-500 Special : False Status : 0 ...
So know we would like to just filter the specific user we want to delete on the local computer. Note that you need two backslashes!
> Get-WmiObject -Class Win32_UserProfile -ComputerName Localhost -Filter "LocalPath='c:\\Users\\User10'" __GENUS : 2 __CLASS : Win32_UserProfile __SUPERCLASS : __DYNASTY : Win32_UserProfile __RELPATH : Win32_UserProfile.SID="S-1-5-21-2737818435-1107748272-1045870659-1117" __PROPERTY_COUNT : 12 __DERIVATION : {} __SERVER : W7-2 __NAMESPACE : root\cimv2 __PATH : \\W7-2\root\cimv2:Win32_UserProfile.SID="S-1-5-21-2737818435-1107748272-1045870659-1117" LastDownloadTime : LastUploadTime : LastUseTime : 20120920084050.857000+000 Loaded : False LocalPath : C:\Users\user10 RefCount : 0 RoamingConfigured : False RoamingPath : RoamingPreference : SID : S-1-5-21-2737818435-1107748272-1045870659-1117 Special : False Status : 0
Perfect that will filter our specific user for us. So lets explore how to delete this profile, save this information to a variable $UserProfile and show what PSbase commands that are available. (Delete() method will not be visible if you use $UserProfile | Get-Member)
> $UserProfile = Get-WmiObject -Class Win32_UserProfile -ComputerName Localhost -Filter "LocalPath='c:\\Users\\User10'" > $UserProfile.PSBase | Get-Member TypeName: System.Management.Automation.PSMemberSet Name MemberType Definition ---- ---------- ---------- Disposed Event System.EventHandler Disposed(System.Object, System.EventArgs) Clone Method System.Object Clone() CompareTo Method bool CompareTo(System.Management.ManagementBaseObject otherObject, S... CopyTo Method System.Management.ManagementPath CopyTo(System.Management.Management... CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType) Delete Method System.Void Delete(), System.Void Delete(System.Management.DeleteOpt... Dispose Method System.Void Dispose() Equals Method bool Equals(System.Object obj) Get Method System.Void Get(), System.Void Get(System.Management.ManagementOpera... GetHashCode Method int GetHashCode() GetLifetimeService Method System.Object GetLifetimeService() GetMethodParameters Method System.Management.ManagementBaseObject GetMethodParameters(string me... GetPropertyQualifierValue Method System.Object GetPropertyQualifierValue(string propertyName, string ... GetPropertyValue Method System.Object GetPropertyValue(string propertyName) GetQualifierValue Method System.Object GetQualifierValue(string qualifierName) GetRelated Method System.Management.ManagementObjectCollection GetRelated(), System.Ma... GetRelationships Method System.Management.ManagementObjectCollection GetRelationships(), Sys... ...
So we have all the information we need, we know what command to use, what WMI class to use, what method to use, so the final script will look to this, without any error control
$UserProfile = Get-WmiObject -Class Win32_UserProfile -ComputerName Localhost -Filter "LocalPath='c:\\Users\\User10'" $UserProfile.Delete()
I have just tested this on Windows 7, and I assume that it will work on Windows 8 and Vista as well!
Nice poost
LikeLiked by 1 person