How to automatically delete ALL user profiles on Windows Server 2008

AID: 4115
  • Status: Published

2850 points

  • Bynimatejic
  • TypeTips/Tricks
  • Posted on2010-11-12 at 10:55:31
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"
                                    
1:

Select allOpen in new window


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
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:

Select allOpen 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:
http://www.wincert.net/tips/windows-server/2285-how-to-completely-delete-a-windows-server-2008-profile-with-script.html
Asked On
2010-11-12 at 10:55:31ID4115
Tags

user profile

,

windows server 2008

,

script

Topic

Windows Server 2008

Views
1719

Comments

Author Comment

by: nimatejic on 2010-11-14 at 21:07:15ID: 21318

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

Regards,
Nik

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Windows Server 2008 Experts

  1. dariusg

    487,253

    Wizard

    1,568 points yesterday

    Profile
    Rank: Genius
  2. hanccocka

    269,517

    Guru

    2,000 points yesterday

    Profile
    Rank: Genius
  3. kevinhsieh

    266,722

    Guru

    0 points yesterday

    Profile
    Rank: Genius
  4. demazter

    188,767

    Guru

    0 points yesterday

    Profile
    Rank: Genius
  5. mkline71

    181,660

    Guru

    2,250 points yesterday

    Profile
    Rank: Genius
  6. motnahp00

    163,470

    Guru

    6,700 points yesterday

    Profile
    Rank: Sage
  7. leew

    144,180

    Master

    0 points yesterday

    Profile
    Rank: Savant
  8. ve3ofa

    131,857

    Master

    0 points yesterday

    Profile
    Rank: Genius
  9. acbrown2010

    114,392

    Master

    0 points yesterday

    Profile
    Rank: Genius
  10. arnold

    101,348

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  11. iSiek

    89,671

    Master

    6,100 points yesterday

    Profile
    Rank: Genius
  12. dstewartjr

    84,405

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  13. Anuroopsundd

    80,040

    Master

    750 points yesterday

    Profile
    Rank: Sage
  14. dvt_localboy

    79,517

    Master

    0 points yesterday

    Profile
    Rank: Sage
  15. alanhardisty

    76,976

    Master

    0 points yesterday

    Profile
    Rank: Genius
  16. newmath

    73,559

    Master

    0 points yesterday

    Profile
    Rank: Master
  17. hhaywood000

    71,824

    Master

    0 points yesterday

    Profile
    Rank: Wizard
  18. Neilsr

    70,519

    Master

    0 points yesterday

    Profile
    Rank: Genius
  19. RobWill

    60,712

    Master

    0 points yesterday

    Profile
    Rank: Savant
  20. Mutawadi

    60,156

    Master

    0 points yesterday

    Profile
    Rank: Guru
  21. dkotte

    56,740

    Master

    0 points yesterday

    Profile
    Rank: Guru
  22. spaperov

    55,822

    Master

    0 points yesterday

    Profile
    Rank: Wizard
  23. radhakrishnan2007

    55,732

    Master

    0 points yesterday

    Profile
    Rank: Wizard
  24. chakko

    53,161

    Master

    0 points yesterday

    Profile
    Rank: Genius
  25. cgaliher

    51,613

    Master

    0 points yesterday

    Profile
    Rank: Genius

Hall Of Fame