Solved

How to Delete a Local Profile After Reboot on Windows 7

Posted on 2010-09-08
16
988 Views
Last Modified: 2012-05-10
Using a GPO to delete local profiles after reboot in Windows 7, but it's only removing the profile in the registry, not in C:\Users\...

Any idea how to get rid of the folders in C:\Users automatically?

Thanks,
0
Comment
Question by:tgarrity
  • 7
  • 6
16 Comments
 
LVL 4

Expert Comment

by:keith_opswat
ID: 33631029
1. Click Start, type GPEDIT.MSC and press ENTER

2. Expand Computer Configuration | Administrative Templates | System

3. Click User Profiles

4. Double-click Delete user profiles older than a specified number of days on system restart

5. Select Enabled, Specify the number of days and click OK.
0
 
LVL 1

Author Comment

by:tgarrity
ID: 33631246
Do you recommed this to be setup locally, or through group policy?
0
 
LVL 1

Author Comment

by:tgarrity
ID: 33631318
Apparently, this setting does not delete the folder that is created in C:\Users, only the registry. Any other ideas?
0
 
LVL 4

Expert Comment

by:keith_opswat
ID: 33631400
I actually have a great idea when I get back to my computer I'll write it out. Gonna create a script for you. Be like 20 min or so
0
 
LVL 1

Author Comment

by:tgarrity
ID: 33639251
Ok, I'm waiting to see what your script looks like.
Thanks,
0
 
LVL 4

Expert Comment

by:keith_opswat
ID: 33668000
I apologize... I work as a System/Network Admin & Network Security at a Software Development company and had a couple important serves crash. I've been working 36 hour days. If only I wasn't salaried... Damn.

But anyways, I'm writing the script right now and will post it today whether it's 100% working or not.

I don't want to post exactly how it works because I'm worried someone will finish it before I do in case I have to do something at work... But anyways, I will post the How-To behind it in case I'm not able to perfect it tonight.

But I'm pretty sure I'll have it working here in an hour or less. Sorry about the delay again man!!!
0
 
LVL 1

Author Comment

by:tgarrity
ID: 33668286
No worries. I appreciate your help!
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Expert Comment

by:keith_opswat
ID: 33668756
I got it man!! So here's the deal... I didn't have a bunch of profiles in my Users folder so I set up directories in a different folder to test on and it worked perfectly. So everything is setup to work from you Users folder.

Here's how it works... Also... IMPORTANT!! Do NOT place this script in the c:\users folder or in the folder of any of the users you want to get rid of.

When you run it

Here's the code...

[code]
::== rmexception.bat
::== Created by Keith Shannon
::== keith@opswat.com
::== 9-13-2010
@echo off
dir /s/b/ad "C:\Users" > allusers.txt
find /v /i "C:\Users\directorytokeep" < allusers.txt > others.txt
find /v /i "C:\Users\directorytokeep" < others.txt > delete.txt
for /f "tokens=*" %%U in (delete.txt) do (rmdir /s /q %%U )
rmdir /s /q "c:\Users\All Users"
:: DONE
pause
[/code]

[code]
dir /s/b/ad "C:\Users" > allusers.txt
 [/code]

First it runs the directory command removing excess stuff from the output and writing the file path output of the dir command to a text file called allusers.txt .

[code]
find /v /i "C:\Users\directorytokeep" < allusers.txt > others.txt
[/code]

Next you use the lines with the find command. What that does is displays all the lines that are NOT equal to whats between the quotations... (thats what the find /v does)

Then all the /i part of the command does is tells it not to worry about case. Just makes it easier to not mess it up.

So find /v /i takes all the paths in the text file created first which has Every single user profile in it and puts all but the one between the quotations in a different text file. This means you want to put a profile you WANT to keep in between the text file.

[code]
find /v /i "C:\Users\directorytokeep" < others.txt > delete.txt
 [/code]

Next it does the same command again in case you want to keep two User profiles. However, if you do not want to keep 2 user profiles you need to remove the second line AND ALSO you need to change the name of the file others.txt to delete.txt. Since you will now be deleting everyone in the file you get after removing one line.

[code]
for /f "tokens=*" %%U in (delete.txt) do (rmdir /s /q %%U )
[code]

This line probably gave me the biggest trouble to figure out how to use it properly. Basically it just says for anything equal to * (which is everything) in the token
(which is set to be the text in the file delete.txt and it runs the command rmdir /s /q before each line inside the text file. SO it deletes everyfolder who has a path to them in the text file. And since it places EVERYONE in the file and you have to remove only the ones you want to keep it will successfully remove any profile from teh computer.

This last line I added in because if the profile name had a space in it (Pretty much the only one that will is All Users. Won't delete because it doesn't catch teh space.

All though I actually think I just figured out how to fix it but I don't have time to test it. So I'll put the change here and you can try it out and see if it works.

[code]
for /f "tokens=*" %%U in (delete.txt) do (rmdir /s /q "%%U" )
[/code]

I just put quotations around each line of text hopefully... I will test it out tomomrrow if you don't wanna try it. If you don't want ot try this yourself and would rather me make 100% sure it will work on the User profiles I will set up an OS in a Virtual Machine and test it out.


This line is for use if the quotations don't work... Only leave this line in if you want to delete the All Users profile.
[code]
rmdir /s /q "c:\Users\All Users"
[/code]

So anyways this should probably be run as administrator... You can use task scheduler to make it run at startup as any account you want. Or if you really really want to push it down from the Domain Controller I can also give you a vbscript I have that will enable you to do the runas command and push both of them down from the domain controller.

All though you can also use the Domain Controller or another batch script I've written to schedule a task on your network computers remotely. You don't have to leave the batch script on the local computer since you can have it pulled in from a shared drive. Also, as a scheduled task unlike the runas vb script I have if they were to somehow get a look at the vb code you have to have your password in the file. So... let me know how you want to do it and I'll help you out.

Also if you need additional help with this script I can do that too. I know this works... Just not 100% if permissions will screw you trying to remove the user profiles. I'll be able to figure it out though :)

Good luck
0
 
LVL 4

Accepted Solution

by:
keith_opswat earned 500 total points
ID: 33668783
Just post on here any answers you have but also mail it to me. Post it on here so I'll remember to reply so anyone in the futre looking for something like this will find the answer without having to ask it again or them not be able to get an answer.

But if you e-mail me the question as well I can help you faster cause my e-mails come on my phone.

Anyways like I said just drop it into a bat and let it rip... I always run the batch from the command prompt instead of double clicking iust so I'm sure I can see all the output.

Hope it works!! I had a fun time figuring out how to do it. People who saw batch scripting isn't really scripting just because they don't know all the tricks to it. Batch scripting may not be as strong as bash or shell, or vbscripting, and nothings as close to perl butif you know how ot work it you can do baller things with batch. Especially if you tie batch and vbs together.

So if you have permissions problems lemme know... Actually I'll post the code for the vb run as. It says how to use it in the code but I recommend you don't use it unless you absolutely have to.

Here you go.. last piece from me for this post. I love scripting and have quite a few scripts that I've put together. Also, if you need something done chances are I won't mind just spending some time on it like this one

Semper Fi

On Error Resume Next
dim WshShell,oArgs,FSO

set oArgs=wscript.Arguments

if InStr(oArgs(0),"?")<>0 then
wscript.echo VBCRLF & "? HELP ?" & VBCRLF
Usage
end if

if oArgs.Count <3 then
wscript.echo VBCRLF & "! Usage Error !" & VBCRLF
Usage
end if

sUser=oArgs(0)
sPass=oArgs(1)&VBCRLF
sCmd=oArgs(2)

set WshShell = CreateObject("WScript.Shell")
set WshEnv = WshShell.Environment("Process")
WinPath = WshEnv("SystemRoot")&"\System32\runas.exe"
set FSO = CreateObject("Scripting.FileSystemObject")

if FSO.FileExists(winpath) then
'wscript.echo winpath & " " & "verified"
else
wscript.echo "!! ERROR !!" & VBCRLF & "Can't find or verify " & winpath &"." & VBCRLF & "You must be running Windows 2000 for this script to work."
set WshShell=Nothing
set WshEnv=Nothing
set oArgs=Nothing
set FSO=Nothing
wscript.quit
end if

rc=WshShell.Run("runas /user:" & sUser & " " & CHR(34) & sCmd & CHR(34), 2, FALSE)
Wscript.Sleep 30 'need to give time for window to open.
WshShell.AppActivate(WinPath) 'make sure we grab the right window to send password to
WshShell.SendKeys sPass 'send the password to the waiting window.

set WshShell=Nothing
set oArgs=Nothing
set WshEnv=Nothing
set FSO=Nothing

wscript.quit

'************************
'* Usage Subroutine *
'************************
Sub Usage()
On Error Resume Next
msg="Usage: cscript|wscript vbrunas.vbs Username Password Command" & VBCRLF & VBCRLF & "You should use the full path where necessary and put long file names or commands" & VBCRLF & "with parameters in quotes" & VBCRLF & VBCRLF &"For example:" & VBCRLF &" cscript vbrunas.vbs quilogy\jhicks luckydog e:\scripts\admin.vbs" & VBCRLF & VBCRLF &" cscript vbrunas.vbs quilogy\jhicks luckydog " & CHR(34) &"e:\program files\scripts\admin.vbs 1stParameter 2ndParameter" & CHR(34)& VBCRLF & VBCRLF & VBCLRF & "cscript vbrunas.vbs /?|-? will display this message."

wscript.echo msg

wscript.quit

end sub
'End of Script
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 33669743
0
 
LVL 1

Author Closing Comment

by:tgarrity
ID: 33672031
Thanks for your time and help on this Keith!
0
 
LVL 4

Expert Comment

by:keith_opswat
ID: 33674482
Hey not a problem man!! Glad I could help... This was actually really fun for me to try and figure a way out to do that. I've never tried to delete all files in a folder except a few when I didn't know what the name of the others were gonna be. Usually I knew what all the files were gonna be named so I could call them specifically without having to do anything weird.

Also, if you don't those text files left over from after the script is run just add a couple lines at the end

del /f allusers.txt etc..etc..

Anyways, if you have any more scripting needs you know where to find me!
0
 
LVL 4

Expert Comment

by:keith_opswat
ID: 33674517
and tbsqadi like 10 people have sent him that same exact info. That doesn't remove the folders in the Users folder.

He wanted to delete all their files for it every time it restarts. I'm guessing theyre public computers so it makes sense.
0
 
LVL 1

Author Comment

by:tgarrity
ID: 33675004
yea theses are public pcs at a library.
0

Featured Post

Why spend so long doing email signature updates?

Do you spend loads of your time carrying out email signature updates? Not very interesting are they? Don’t let signature updates get you down. Let Exclaimer Cloud - Signatures for Office 365 make managing email signatures a breeze.

Join & Write a Comment

Several part series to implement Internet Explorer 11 Enterprise Mode
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now