Site icon WinCert

Completely Delete User Profiles with a Script

<p><a href&equals;"http&colon;&sol;&sol;wincert&period;net&sol;wp-content&sol;uploads&sol;2015&sol;01&sol;windows-server2&period;jpg"><img class&equals;"alignnone size-full wp-image-551" src&equals;"http&colon;&sol;&sol;wincert&period;net&sol;wp-content&sol;uploads&sol;2015&sol;01&sol;windows-server2&period;jpg" alt&equals;"Windows Server&comma;access&comma;permission&comma;ipsec fails&comma;printers offline&comma;printer installation&comma;trusted sites&comma;item&comma;installation file missing&comma;user profiles" width&equals;"720" height&equals;"340" &sol;><&sol;a><&sol;p>&NewLine;<p>Here&&num;8217&semi;s a useful script that will automatically delete ALL user profiles that are on a Windows Server 2008&sol;R2 computer&period;<&sol;p>&NewLine;<p><strong>WARNING&excl;<&sol;strong><br &sol;>&NewLine;This is a dangerous operation&comma; designed for use by an administrator who needs to do a complete purge&semi; for instance at the end of a semester&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;ff0000&semi;"><span style&equals;"text-decoration&colon; underline&semi;"> Note<&sol;span>&colon; <&sol;span><br &sol;>&NewLine;For purposes of this example&comma; we will use the following path for the script files&colon;<br &sol;>&NewLine;<strong>D&colon;&bsol;Scripts<&sol;strong><&sol;p>&NewLine;<ul>&NewLine;<li>Copy the below provided code into the notepad and save it as <strong>delete&lowbar;profiles&period;vbs in D&colon;&bsol;Scripts <&sol;strong><&sol;li>&NewLine;<&sol;ul>&NewLine;<ul>&NewLine;<li>Create a new notepad file and copy the following line&colon;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span class&equals;"Apple-style-span" style&equals;"line-height&colon; normal&semi; -webkit-border-horizontal-spacing&colon; 2px&semi; -webkit-border-vertical-spacing&colon; 2px&semi; font-size&colon; medium&semi;"><span style&equals;"color&colon; &num;339966&semi;">cscript&period;exe &&num;8220&semi;D&colon;&bsol;Scripts&bsol;delete&lowbar;profiles&period;vbs SRVNAME&&num;8221&semi; &gt&semi; &&num;8220&semi;D&colon;&bsol;Scripts&bsol;profile&lowbar;delete&period;txt&&num;8221&semi;<&sol;span><br &sol;>&NewLine;<&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li>Where <em>SRVNAME<&sol;em> is the name of the server where you want to delete the profiles&period;The redirection &lpar;<strong>&gt&semi;<&sol;strong>&rpar; in that command line acts to create &lpar;or append&rpar; a text file named <strong>profile&lowbar;delete&period;txt<&sol;strong> that will act to log the deletions&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<ul>&NewLine;<li>Save it as <strong>delete&lowbar;all&lowbar;profiles&period;bat<&sol;strong> and save it in <strong>D&colon;&bsol;Scripts<&sol;strong><&sol;li>&NewLine;<&sol;ul>&NewLine;<ul>&NewLine;<li>Create a scheduled job and run <strong>delete&lowbar;all&lowbar;profiles&period;bat<&sol;strong> at the desired time&period;<br &sol;>&NewLine;I suggest using a batch file and setting up a scheduled task as that lets the script run with the necessary permissions&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Here is the code for the script file&colon; <strong>delete&lowbar;profiles&period;vbs<&sol;strong><&excl;--more--><&sol;p>&NewLine;<pre id&equals;"codeSnippet1912641" style&equals;"font-size&colon; 12px&semi; padding&colon; 0px&semi; margin&colon; 0px&semi;">On Error Resume Next<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;><br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>args &equals; WScript&period;Arguments&period;Count<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;><br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>If args &lt&semi;&gt&semi; 1 Then<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>  WScript&period;Echo "usage&colon; delete&lowbar;profiles SVRNAME"<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>  WScript&period;Echo "example &lpar;for remote profiles&rpar;&colon; cscript&period;exe delete&lowbar;profiles SOMESERVER  "<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>  WScript&period;Echo "example &lpar;for local profiles&rpar;&colon; cscript&period;exe delete&lowbar;profiles &period;  "<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>  WScript&period;Quit<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>End If<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;><br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>strComputer &equals; WScript&period;Arguments&period;Item&lpar;0&rpar;<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>Set objWMIService &equals; GetObject&lpar;"winmgmts&colon;&bsol;&bsol;" &amp&semi; strComputer &amp&semi;"&bsol;root&bsol;cimv2"&rpar;<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>Set colProfiles &equals; objWMIService&period;ExecQuery&lpar;"Select &ast; from Win32&lowbar;UserProfile"&rpar;<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>Wscript&period;Echo "&equals;&equals;" &amp&semi; WScript&period;Arguments&period;Item&lpar;0&rpar; &amp&semi; "&equals;&equals;" &amp&semi; vbNewLine<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;><br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>For Each objProfile in colProfiles<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>  Set objSID &equals; objWMIService&period;Get&lpar;"Win32&lowbar;SID&period;SID&equals;'" &amp&semi; objProfile&period;SID &amp&semi;"'"&rpar;<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>      If &lpar;objSID&period;ReferencedDomainName &equals; "DOMAIN NAME"&rpar; Then<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>      If Not &lpar;&lpar;objSID&period;AccountName &equals; "USERNAME TO EXCLUDE"&rpar; Or &lpar;Left &lpar;objSID&period;AccountName&comma;2&rpar; &equals; "USERNAME PREFIX TO EXCLUDE"&rpar;&rpar; Then<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>          Set objUserProfile &equals; GetObject&lpar;"winmgmts&colon;&lbrace;impersonationlevel&equals;impersonate&rcub;&excl;&bsol;&bsol;" &lowbar;<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>          &amp&semi; strComputer &amp&semi;"&bsol;root&bsol;cimv2&colon;Win32&lowbar;UserProfile&period;" &lowbar;<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>          &amp&semi;"SID&equals;'" &amp&semi; objProfile&period;Sid &amp&semi;"'"&rpar;<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>          objUserProfile&period;Delete&lowbar;<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>     Wscript&period;Echo objSID&period;AccountName &amp&semi; "&semi;" &amp&semi; objSID&period;ReferencedDomainName &amp&semi; "&semi;" &amp&semi; objProfile&period;LocalPath &amp&semi; " - " &amp&semi; "DELETED"<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>     End If<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>End If<br style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;" &sol;>Next<&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;ff0000&semi;">NOTES<&sol;span>&colon; In <strong style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;">line 19<&sol;strong> you have to specify the domain name to be used in the script and in <strong style&equals;"padding&colon; 0px&semi; margin&colon; 0px&semi;">line 20<&sol;strong> you can specify user accounts that shouldn&&num;8217&semi;t be deleted&comma; like Administrator accounts&period;<&sol;p>&NewLine;<p>Courtesy&colon; KresimiK<&sol;p>&NewLine;

Exit mobile version