<p><a href="http://wincert.net/wp-content/uploads/2015/01/windows-server2.jpg"><img class="alignnone size-full wp-image-551" src="http://wincert.net/wp-content/uploads/2015/01/windows-server2.jpg" alt="Windows Server,access,permission,ipsec fails,printers offline,printer installation,trusted sites,item,installation file missing,user profiles" width="720" height="340" /></a></p>
<p>Here&#8217;s a useful script that will automatically delete ALL user profiles that are on a Windows Server 2008/R2 computer.</p>
<p><strong>WARNING!</strong><br />
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.</p>
<p><span style="color: #ff0000;"><span style="text-decoration: underline;"> Note</span>: </span><br />
For purposes of this example, we will use the following path for the script files:<br />
<strong>D:\Scripts</strong></p>
<ul>
<li>Copy the below provided code into the notepad and save it as <strong>delete_profiles.vbs in D:\Scripts </strong></li>
</ul>
<ul>
<li>Create a new notepad file and copy the following line:</li>
</ul>
<p><span class="Apple-style-span" style="line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; font-size: medium;"><span style="color: #339966;">cscript.exe &#8220;D:\Scripts\delete_profiles.vbs SRVNAME&#8221; >; &#8220;D:\Scripts\profile_delete.txt&#8221;</span><br />
</span></p>
<ul>
<li>Where <em>SRVNAME</em> is the name of the server where you want to delete the profiles.The redirection (<strong>>;</strong>) in that command line acts to create (or append) a text file named <strong>profile_delete.txt</strong> that will act to log the deletions.</li>
</ul>
<ul>
<li>Save it as <strong>delete_all_profiles.bat</strong> and save it in <strong>D:\Scripts</strong></li>
</ul>
<ul>
<li>Create a scheduled job and run <strong>delete_all_profiles.bat</strong> at the desired time.<br />
I suggest using a batch file and setting up a scheduled task as that lets the script run with the necessary permissions.</li>
</ul>
<p>Here is the code for the script file: <strong>delete_profiles.vbs</strong><!--more--></p>
<pre id="codeSnippet1912641" style="font-size: 12px; padding: 0px; margin: 0px;">On Error Resume Next<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />args = WScript.Arguments.Count<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />If args <;>; 1 Then<br style="padding: 0px; margin: 0px;" /> WScript.Echo "usage: delete_profiles SVRNAME"<br style="padding: 0px; margin: 0px;" /> WScript.Echo "example (for remote profiles): cscript.exe delete_profiles SOMESERVER "<br style="padding: 0px; margin: 0px;" /> WScript.Echo "example (for local profiles): cscript.exe delete_profiles . "<br style="padding: 0px; margin: 0px;" /> WScript.Quit<br style="padding: 0px; margin: 0px;" />End If<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />strComputer = WScript.Arguments.Item(0)<br style="padding: 0px; margin: 0px;" />Set objWMIService = GetObject("winmgmts:\\" &; strComputer &;"\root\cimv2")<br style="padding: 0px; margin: 0px;" />Set colProfiles = objWMIService.ExecQuery("Select * from Win32_UserProfile")<br style="padding: 0px; margin: 0px;" />Wscript.Echo "==" &; WScript.Arguments.Item(0) &; "==" &; vbNewLine<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />For Each objProfile in colProfiles<br style="padding: 0px; margin: 0px;" /> Set objSID = objWMIService.Get("Win32_SID.SID='" &; objProfile.SID &;"'")<br style="padding: 0px; margin: 0px;" /> If (objSID.ReferencedDomainName = "DOMAIN NAME") Then<br style="padding: 0px; margin: 0px;" /> If Not ((objSID.AccountName = "USERNAME TO EXCLUDE") Or (Left (objSID.AccountName,2) = "USERNAME PREFIX TO EXCLUDE")) Then<br style="padding: 0px; margin: 0px;" /> Set objUserProfile = GetObject("winmgmts:{impersonationlevel=impersonate}!\\" _<br style="padding: 0px; margin: 0px;" /> &; strComputer &;"\root\cimv2:Win32_UserProfile." _<br style="padding: 0px; margin: 0px;" /> &;"SID='" &; objProfile.Sid &;"'")<br style="padding: 0px; margin: 0px;" /> objUserProfile.Delete_<br style="padding: 0px; margin: 0px;" /> Wscript.Echo objSID.AccountName &; ";" &; objSID.ReferencedDomainName &; ";" &; objProfile.LocalPath &; " - " &; "DELETED"<br style="padding: 0px; margin: 0px;" /> End If<br style="padding: 0px; margin: 0px;" />End If<br style="padding: 0px; margin: 0px;" />Next</pre>
<p><span style="color: #ff0000;">NOTES</span>: In <strong style="padding: 0px; margin: 0px;">line 19</strong> you have to specify the domain name to be used in the script and in <strong style="padding: 0px; margin: 0px;">line 20</strong> you can specify user accounts that shouldn&#8217;t be deleted, like Administrator accounts.</p>
<p>Courtesy: KresimiK</p>