[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


How to automatically delete ALL user profiles on Windows Server 2008

Published on
15,373 Points
1 Endorsement
Last Modified:
Here's a useful script that will automatically delete ALL user profiles that are on a Windows Server 2008/R2 computer.
This is a dangerous operation, designed for use by an administrator who needs to do a complete purge; for instance at the end of a semester.
For purposes of this example, we will use the following path for the script files:
Copy the below provided code into the notepad and save it as delete_profiles.vbs in D:\Scripts
Create a new notepad file and copy the following line:
cscript.exe "D:\Scripts\delete_profiles.vbs SRVNAME" > "D:\Scripts\profile_delete.txt"Where SRVNAME is the name of the server where you want to delete the profiles.

The redirection (>) in that command line acts to create (or append) a text file named profile_delete.txt that will act to log the deletions.
Save it as delete_all_profiles.bat and save it in D:\Scripts
Create a scheduled job and run delete_all_profiles.bat at the desired time.
I suggest using a batch file and setting up a scheduled task as that lets the script run with the necessary permissions.
Here is the code for the script file: delete_profiles.vbs
On Error Resume Next

args = WScript.Arguments.Count

If args <> 1 Then
  WScript.Echo "usage: delete_profiles SVRNAME"
  WScript.Echo "example (for remote profiles): cscript.exe delete_profiles SOMESERVER  "
  WScript.Echo "example (for local profiles): cscript.exe delete_profiles .  "
End If

strComputer = WScript.Arguments.Item(0)
Set objWMIService = GetObject("winmgmts:\\" & strComputer &"\root\cimv2")
Set colProfiles = objWMIService.ExecQuery("Select * from Win32_UserProfile")
Wscript.Echo "==" & WScript.Arguments.Item(0) & "==" & vbNewLine

For Each objProfile in colProfiles
  Set objSID = objWMIService.Get("Win32_SID.SID='" & objProfile.SID &"'")
      If (objSID.ReferencedDomainName = "DOMAIN NAME") Then
      If Not ((objSID.AccountName = "USERNAME TO EXCLUDE") Or (Left (objSID.AccountName,2) = "USERNAME PREFIX TO EXCLUDE")) Then
          Set objUserProfile = GetObject("winmgmts:{impersonationlevel=impersonate}!\\" _
	  & strComputer &"\root\cimv2:Win32_UserProfile." _
          &"SID='" & objProfile.Sid &"'")
     Wscript.Echo objSID.AccountName & ";" & objSID.ReferencedDomainName & ";" & objProfile.LocalPath & " - " & "DELETED"
     End If
End If

Open in new window

NOTES: In line 19 you have to specify the domain name to be used in the script and in line 20 you can specify user accounts that shouldn't be deleted, such as Administrator accounts.

Originally posted on my web site and used with my permission:
LVL 17

Author Comment

Yes, I am the original author and this is my site and article ;).


Expert Comment

by:abdullah eideh
when i executed script this error show in file  profile_delete.txt
Input Error: There is no script engine for file extension ".vbs java".
LVL 87

Expert Comment

Follow up question is here: https://www.experts-exchange.com/questions/28984939/There-is-no-script-engine-for-file-extension-vbs.html#a41899182 . Please don't post any more comment, as this is an article, not a question

Featured Post

Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month