How do I delete a bak file from the registry with a script?

Good afternoon,
I'm need some help with a script I'm using to clean up the profiles on the public pc's. I need to add some code to remove .bak files from the profile list. particularly this one, S-1-5-21-3822824043-2368925748-477173625-1177.bak For that matter, I could use a totally different script but I would prefer to just add it to this one.



@ECHO OFF

FOR /F "tokens=*" %%a IN ('REG QUERY "HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList"^|FIND /I "S-1-5-21"') DO CALL :REGDELETE "%%a"
GOTO CLEAN

:REGDELETE
FOR /F %%b IN ('REG QUERY %1 /V ProfileImagePath^|FIND /I "internet"') DO REG DELETE "%%b" /F
FOR /F %%c IN ('REG QUERY %1 /V ProfileImagePath^|FIND /I "TEMP"') DO REG DELETE "%%c" /F
FOR /F %%d IN ('REG QUERY %1 /V ProfileImagePath^|FIND /I "short"') DO REG DELETE "%%d" /F
FOR /F %%e IN ('REG QUERY %1 /V ProfileImagePath^|FIND /I "jobseeker"') DO REG DELETE "%%e" /F
FOR /F %%f IN ('REG QUERY %1 /V ProfileImagePath^|FIND /I "catalog"') DO REG DELETE "%%f" /F
GOTO CLEAN

:CLEAN
FOR /F %%b IN ('DIR C:\Users /B^|FIND /I "internet"') DO RMDIR /S /Q "C:\Users\%%b"
FOR /F %%c IN ('DIR C:\Users /B^|FIND /I "TEMP"') DO RMDIR /S /Q "C:\Users\%%c"
FOR /F %%d IN ('DIR C:\Users /B^|FIND /I "short"') DO RMDIR /S /Q "C:\Users\%%d"
FOR /F %%e IN ('DIR C:\Users /B^|FIND /I "jobseeker"') DO RMDIR /S /Q "C:\Users\%%e"
FOR /F %%f IN ('DIR C:\Users /B^|FIND /I "public"') DO RMDIR /S /Q "C:\Users\%%f"
FOR /F %%g IN ('DIR C:\Users /B^|FIND /I "catalog"') DO RMDIR /S /Q "C:\Users\%%g"

GOTO CLEANGPO

:CLEANGPO
DEL "C:\Users\All Users\Microsoft\Group Policy" /F /Q
REG DELETE "HKLM\Software\Microsoft\Windows\CurrentVersion\Group Policy" /F

:EOF



Thanks!
Regards,
ABBEadmin
Donna JohnsonAsst. Systems ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Darrell PorterEnterprise Business Process ArchitectCommented:
I see where you're cleansing HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList, but where are you cleaning out HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileGuid?

You'll essentially need to clean this key of *.BAK entries as well.  You'll need to iterate through ProfileGuid to determine if the SidString values of each key match one of the key names in ProfileList and have ".bak" as the last 4 characters.
Donna JohnsonAsst. Systems ManagerAuthor Commented:
Thanks. I found the associated key. Is that what caused the .bak to be created? What do I need to add to my script to keep this from happening?

HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-3822824043-2368925748-477173625-1177.bak
HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileGuid\{26c1a49a-05e1-4300-b933-aea2158802c9}
Shaun VermaakTechnical SpecialistCommented:
Use PowerShell
Get-ChildItem -Path "hklm:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"|Where Name -like "*.bak"|Remove-Item

Open in new window

Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Donna JohnsonAsst. Systems ManagerAuthor Commented:
Can you tell me how to turn it to vbs?

Regards,
ABBEadmin
Bill PrewIT / Software Engineering ConsultantCommented:
@Donna,

Do you want a BAT version of the Powershell that was presented, or do you now want a VBS version?

Your original script was a BAT file, but I don't want to show the modifications to that if you no longer want BAT and need VBS.


»bp
Donna JohnsonAsst. Systems ManagerAuthor Commented:
Modifications to the BAT file would be great. Thanks!
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, take a look at this and see if it makes sense.

I streamlined your original file a bit to reduce code, goto's and call's.  I also added in the *.bak removal.

Notice the SET DEBUG=ECHO line near the top.  Since this script will be making changes to the registry it's hard to test it just by running it.  It's pretty hard to undo deletes to the registry.  So I added the DEBUG setting, which as you look further down the script you will see I prefaced each action line that deletes something with that variable.  Placing ECHO in that variable means each action line will only be echo'ed to the console what would have been executed, but won't actually execute the actual delete command.

Run it, see if the statements it displays are what you would want.  Once they are then just change the SET near the top to:

SET DEBUG=

and run for real.  It will then execute the actual delete commands.  Let me know what questions you have.

@ECHO OFF
SETLOCAL

REM Enable debug mode
SET DEBUG=ECHO

REM Define list of profiles to remove
SET DelNodes=internet temp short jobseeker catalog

REM Remove any backup nodes from registry
FOR /F "tokens=*" %%a IN ('REG QUERY "HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList" /F "S-1-5-21*.bak"^|FIND /I "HKLM_"') DO (
    %DEBUG% REG DELETE "%%a" /F
)

REM Remove desired profiles from registry
FOR /F "tokens=*" %%a IN ('REG QUERY "HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList"  /F "S-1-5-21*"^|FIND /I "HKLM_"') DO (
    FOR %%b in (%DelNodes%) DO (
        FOR /F %%c IN ('REG QUERY %%a /V ProfileImagePath^|FIND /I "%%b"') DO (
            %DEBUG% REG DELETE "%%c" /F
        )
    )
)

REM Remove user profile sub-directories
FOR %%b in (%DelNodes%) DO (
    FOR /F %%c IN ('DIR C:\Users /B^|FIND /I "%%b"') DO (
        %DEBUG% RMDIR /S /Q "C:\Users\%%c"
    )
)

REM Remove GPO
%DEBUG% DEL "C:\Users\All Users\Microsoft\Group Policy" /F /Q
%DEBUG% REG DELETE "HKLM\Software\Microsoft\Windows\CurrentVersion\Group Policy" /F

Open in new window


»bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Donna JohnsonAsst. Systems ManagerAuthor Commented:
Thanks Guys!
Donna JohnsonAsst. Systems ManagerAuthor Commented:
Thanks Bill. That's exactly what I needed.
Bill PrewIT / Software Engineering ConsultantCommented:
Great, glad that helped.


»bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.