Vista Delete user profiles prompt

July 2, 2009 at 08:29:29
Specs: Vista,2008, na
Hello I'm writing a batch that will connect to a remote vista computer, list the roaming profiles and enable user to select the profile they want deleted. (using wmic and Win32_UserProfile)

I have already a script that deletes all of the Roaming profiles,

that script is here :

TITLE %~nx0
@Echo on
cls
color f0

@echo .
Set /p TargetCmp=Enter computername on which we will delete all the roaming profiles:

echo Target computer name equ %TargetCmp%

for /f "tokens=1,2" %%a in ('wmic /node:"%TargetCmp%" path win32_UserProfile get RoamingConfigured^,Sid^|find "TRUE"') do (
echo %%a echo %%b
wmic /node:"%TargetCmp%" path win32_UserProfile where Sid="%%b" Delete
)
pause

Exit

I know would like to create a new script that will basical 1st display the profile and lets the operator just select one profile to be deleted.

the following cmd : wmic /node:%computername% path win32_UserProfile where RoamingConfigured="TRUE" get Localpath,sid,loaded

gives the following output :

Loaded LocalPath SID
FALSE C:\Users\user1 S-1-5-21-594694905-1720275925-1234779376-33861
FALSE C:\Users\User2 S-1-5-21-594694905-1720275925-1234779376-28753
FALSE C:\Users\User3 S-1-5-21-594694905-1720275925-1234779376-11962

My question is : I've added the following changed the script to the following :

TITLE %~nx0
@Echo on
cls
color f0

setlocal ENABLEDELAYEDEXPANSION

@echo .
Set /p TargetCmp=Enter computername on which we will delete all the roaming profiles:

echo Target computer name equ %TargetCmp%

for /f "tokens=1-3" %%a in ('wmic /node:"%TargetCmp%" path win32_UserProfile where RoamingConfigured^="TRUE" get Loaded^,LocalPath^,sid') do (
echo %%a echo %%b %%c
Choice /M "Press Y to delete the user profile for %%b or N for NO"
Echo the choice is %ErrorLevel%
if %ErrorLevel% == 1 wmic /node:"%TargetCmp%" path win32_UserProfile where Sid="%%c" Delete
)

pause

Exit
The problem I'm having is that used the ErrorLevel result for the choice cmd is always Zero in when it should be 1 for Y or 2 for N. Any ideas of what I'm going wrong ?

thank you.

Naim.


See More: Vista Delete user profiles prompt

Report •


#1
July 2, 2009 at 14:49:39
I solved my problem by using !ErrorLevel! instead of %ErrorLevel%

Here is the final verison of my script

TITLE %~nx0
@Echo off
cls
color 74


setlocal ENABLEDELAYEDEXPANSION

@echo .
@echo This script will connect will prompt for a remote computer
@echo and give you the option to delete all users profile or prompt to delete selected profiles.
@echo .
@echo You must be part of the Desktop User AD group for this script to work.
@echo This script will only work on Target Vista and 2008 Servers.
@echo .

@echo .
Set /p TargetCmp=Enter the remote computer name:

echo Target computer name equ %TargetCmp%

@echo .
@echo Press A to delete all roaming profiles on %TargetCmp%
@echo Press P to prompt before deleting Profiles one at a time.
@echo .

Choice /C AP
if %ErrorLevel% == 2 Goto DelPrompt
if %ErrorLevel% == 1 Goto DelAll


:DelPrompt

for /f "tokens=1-3" %%a in ('wmic /node:"%TargetCmp%" path win32_UserProfile where RoamingConfigured^="TRUE" get Loaded^,LocalPath^,sid^|find "C:"') do (
Choice.exe /C YN /M "Press Y to delete the user profile for %%b or N for NO"
if !ErrorLevel! == 1 wmic /node:"%TargetCmp%" path win32_UserProfile where Sid="%%c" Delete
)

EndLocal
pause
Exit


:DelAll
for /f "tokens=1-3" %%a in ('wmic /node:"%TargetCmp%" path win32_UserProfile where RoamingConfigured^="TRUE" get Loaded^,LocalPath^,sid^|find "C:"') do (
echo Deleting user profile for : %%b with SID : %%c
wmic /node:"%TargetCmp%" path win32_UserProfile where Sid="%%c" Delete
)

EndLocal
pause
Exit


Report •
Related Solutions


Ask Question