Completely Delete User Profiles with a Script
Here’s a useful script that will automatically delete ALL user profiles that are on a Windows Server 2008/R2 computer.
WARNING!
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.
Note:
For purposes of this example, we will use the following path for the script files:
D:\Scripts
- 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 . "
WScript.Quit
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 &"'")
objUserProfile.Delete_
Wscript.Echo objSID.AccountName & ";" & objSID.ReferencedDomainName & ";" & objProfile.LocalPath & " - " & "DELETED"
End If
End If
Next
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, like Administrator accounts.
Courtesy: KresimiK
I get the following error (in the .txt file)when running the script:
“Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
Input Error: There is no script engine for file extension “.vbs WM-ROSWELL”.
Where “WM-ROSWELL” is the name of the server.
Hi Matt,
you’re trying to run the following command right.
cscript.exe nameofthevbs.vbs WM-ROSWELL
Have you changed the script part where you have to enter the domain name?
Yes, but I used the NetBIOS name rather than the FQDN.
Does this need to be run on the local server, or can I do this from my workstation?
Thanks for the help!
You can do this from your workstation too.
Is it working for you now Matt?
No it is not. I can email you the files (.vbs and .bat) I am using if you would like…
Please upload your file somewhere and post a link here.
I will check it out..
http://tinyurl.com/westmtech
Check if you have admin permissions on the target machine.
Make sure to run script like this:
Cscript.exe delete_profiles.vbs TARGETMACHINE > deleted.txt
Also, run CMD in elevated mode if you have UAC turned on.
Where do I set those permissions?
Do I just make a local admin account that matches my domain account name?
Your domain account has to be local administrator on the target machine.
I’ve tried the script and the script successfully deleted all of the profiles that currently were not in use on the target machine.
I got it to work! I needed to take the quotes out of the .bat file…duh moment. I am by no means a script guru. It successfully worked on two terminal servers!
Thanks again!
How do I do this for multiple usernames I want to ignore?
And additionally is it easy to add a function such as (older than 7 days)? now that I’ve found your script classes have started this week and I need to delete all the student accounts older than a week ago.
I am trying to get this to work but the profiles do not appear to be deleting. In the log file, the output only shows the server name. Any thoughts?
Hi Adam,
Script seems fine. Do you have Admin Permission on the target machine?
Please check that you did everything right.
You can also attach your script so I can download and check.
Thanks for sharing nice techie info for ours.. keep posted buddy….
I dont know if the author of the code still active. I am getting error message – Microsoft VBScript compilation error line 18 char 66 expected ‘)’ code:800A03EE
server os:2008 r2
Also will it be possible to exclude the Service account and Network accounts along with the Local Administratoe
I found the issue actually there is extra single quote and double quote which was causing the issue.
the script deletes profile from C:\Users which is good. I would like to know will it touches the local administrator account? and if users logged on to the server will it deletes their profile?
Scripts works fine without any issue on my test server, as i will be using to clear it on Citrix host server I would like to know more information
Thanks
I’m quite sure it shouldn’t delete profile from the local admin. Also if the user is logged on that profile is locked and thus can’t be deleted.
BTW, why not running the script during the night to avoid having any issues with already logged users?
Hi Nik, Thanks for replying back. Yes, i tested it its not deleting the local Admin and logged in user.Yes, that what I planned it for running the script Out of hours.
But I work for healthcare so 24/7. Thanks for replying back.
Hi Nik, I tested the script on one of the production server. It execute the script but nothing happen. no error message nothing. Any idea?
Did the same with the Test server. But on the production it does nothing.
No idea Prabhu, this script was used only for our 2008R2 server environment. Now we’re using Citrix profiler so we don’t have the need to delete local user profiles.
And its weird it works only when the Citrix server is on Maintenance mode