Disable all local Windows accounts - windows

I need to make a batch script that disable all local user accounts of a computer or a way to list all the users, one below another.
I try to generate a file with the command:
net user > %systemroot%\users.txt | findstr /I adm
but the command returns me all users in the same line.
If someone knows how to generate every user in a separate line, it helps me too.

Here's a working code snippet:
for /F "tokens=1* delims==" %%G in (
'wmic USERACCOUNT where "Name!='Administrator'" get Name /value'
) do for /F "tokens=*" %%x in ("%%H") do (
echo %%x
rem some further command(s) here
)
Where the for loops are
%%G to retrieve the Name value (in the second token, %%H);
%%x to remove the ending carriage return in the value returned (wmic behaviour: each output line ends with 0x0D0D0A instead of common 0x0D0A), see the example below.
Example (copy & paste from my command line window):
==>for /F "tokens=1* delims==" %G in ('wmic USERACCOUNT where "Name!='Administra
tor'" get Name /value') do #echo "%H"
""
""
"Guest
""
""
"Edgar
""
""
"Allan
""
""
"Poe
""
""
""
==>for /F "tokens=1* delims==" %G in ('wmic USERACCOUNT where "Name!='Administra
tor'" get Name /value') do #for /F "tokens=*" %x in ("%H") do #echo "%x"
"Guest"
"Edgar"
"Allan"
"Poe"
==>

I don't believe net.exe can output them as separate lines, but you can use a call statement with shift to break the line into separate variables.
for /f "delims=" %%a in ('net user ^| findstr /i adm') do set userlist=%%a
call :process %userlist%
goto :eof
:process
if {%1}=={} goto :eof
::-----------
rem Do something with %1 here
echo %1
::-----------
shift
goto :process

here is the code that solved to me:
for /f "delims=" %%a in ('net user ^| findstr -V "Contas de usu" ^| findstr -V "Comando conc" ^| findstr -V "\-" ^| findstr -V "User accounts" ^| findstr -V "The command"') do (
for %%b in (%%a) do (
if not "%%b" == "Administrador" (
if not "%%b" == "Administrator" (
if not "%%b" == "Convidado" (
Net user %%b /active:no
) else (
for /f "delims=" %%c in ('ver ^| findstr /i "5\.1\."') do (
set version=%%c
)
if not {"!version!"} == {""} (
Net user "Convidado" /active:yes
) else (
Net user "Convidado" /active:no
)
)
) else (
Net user "Administrator" /active:yes
)
) else (
Net user "Administrador" /active:yes
)
)
)

WMIC USERACCOUNT WHERE localaccount=true set disabled=true
run it with admin privileges.

Related

Echo is not working correctly in my Windows Batch script

Main Problem:
The ECHO command ignores all characters before the token argument and inserts all text after the token argument before the token in the output:
Here's the script:
#ECHO OFF
FOR /F "USEBACKQ TOKENS=2 DELIMS==" %%a IN (`
WMIC NIC WHERE
"MACAddress IS NOT NULL"
GET
MACAddress
/FORMAT:LIST ^|
FINDSTR /V /R "^%"
`) DO (
ECHO [%%a]
ECHO 0123456789%%a9876543210
)
PAUSE > NUL
Here's the expected output:
[AA:BB:CC:11:22:33]
0123456789AA:BB:CC:11:22:339876543210
Here's the actual output:
]AA:BB:CC:11:22:33
9876543210AA:BB:CC:11:22:33
Why do I care?
My intention is to replace the DO () block in the example above with this block:
:: ...FOR LOOP STUFF...
`) DO (
WMIC NIC WHERE ^
"MACAddress=%%a" ^
GET ^
NetConnectionID ^
/FORMAT:LIST | FINDSTR /V /R "^$"
WMIC NICCONFIG WHERE ^
"MACAddress=%%a" ^
GET ^
DefaultIPGateway,^
DNSServerSearchOrder,^
IPAddress,^
IPSubnet,^
MacAddress ^
/FORMAT:LIST | FINDSTR /V /R "^$"
)
As described above, all characters before %%a get cut off and all characters after %%a show up before %%a when the script is processed. Somebody please help, I'm figuratively crying here.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=2 DELIMS==" %%a IN ('
WMIC NIC WHERE "MACAddress IS NOT NULL" GET MACAddress
/FORMAT:LIST
') DO (
SET "macaddr=%%a"
SET "macaddr=!macaddr:~0,-1!"
ECHO [!macaddr!]
ECHO 0123456789!macaddr!9876543210
)
GOTO :EOF
I could find no reason for the findstr - as posted, the code gave me no output at all. Removing the findstr provided the reported output.
The wmic quirk can be observed by redirecting wmic output to a file and examining the result with a hex-editor. The cure is to simply mechanically remove the last character of the value assigned to the token.
I simply use Getmac command like in this script :
#echo off
mode 80,5 & Color 9E
Title Get (LAN ,Public) (IP) and MAC Addresses by Hackoo 2017
cls
echo( & echo(
echo Please Wait a While ... Searching for (LAN ,Public)(IP) and MAC addresses ...
#for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do (
set "LAN_IP=%%a"
)
for /f "tokens=2 delims=: " %%A in (
'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
) Do set ExtIP=%%A
#For /f %%a in ('getmac /NH /FO Table') do (
#For /f %%b in ('echo %%a') do (
If /I NOT "%%b"=="N/A" (
Set "MY_MAC=%%b"
)
)
)
Cls
echo(
echo My Private LAN IP : %LAN_IP%
echo My External Public IP : %ExtIP%
echo MAC Address : %MY_MAC%
Timeout /T 5 /NoBreak>nul & Exit

How can I automatically uninstall all programs containing "VNC" in their displayname using a batch file?

I am attempting to create a batch script that will query the registry for uninstall packages. The goal is to create a batch script that will uninstall all VNC programs (UltraVNC, RealVNC, and TightVNC). This must be compatible with both Windows XP and Windows 7.
I started with the script from this StackOverflow solution Reg Query script for batch script . I am trying to alter the code to allow it to search for "VNC" in the DisplayName, strip the text between the {} in its corresponding UninstallPath, and then run msiexec.exe /qn /x {package-identifier} to silently uninstall the program.
When running the following code, it retrieves several programs that do not contain "VNC" in their DisplayName. The issue appears to lie in the following line of code:
if not "x!str1:VNC=!"=="x!str1!" (
Source of this line of code: How can I check if a variable contains another variable within a windows batch file?
I have tried the following code found at as an alternative, but it also listed way too many packages and did not properly pick only packages with "VNC" in their names.
set str1 = !product[%counter%]!
echo str1 > temp.dat
findstr /c:"VNC" "temp.dat" >nul 2>&1
if %errorlevel% == 0 (
Note: Many of the echo commands are there simply for testing purposes.
#echo off
setlocal
set regVar=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
set excluded=/c:" Microsoft" /c:"MDOP" /c:"Dell"
set count=1
for /f "tokens=1,2,*" %%a in ('Reg Query %regVar% /S ^| findstr "DisplayName HKEY_ UninstallString"') do (
setlocal EnableDelayedExpansion
for %%n in (!count!) do (
ENDLOCAL
SET HKEY=Y
IF "%%a"=="DisplayName" SET "HKEY="&set product[%%n]=%%c
IF "%%a"=="UninstallString" SET "HKEY="&IF NOT DEFINED uninstall[%%n] set uninstall[%%n]=%%c
IF "%%a"=="QuietUninstallString" SET "HKEY="&IF NOT DEFINED uninstall[%%n] set uninstall[%%n]=%%c
IF DEFINED hkey IF DEFINED product[%%n] IF defined uninstall[%%n] SET /a count+=1&SET "hkey="
IF DEFINED hkey set "product[%%n]="&SET "uninstall[%%n]="
)
)
IF NOT DEFINED product[%count%] SET "uninstall[%count%]="&SET /a count-=1
IF NOT DEFINED uninstall[%count%] SET "product[%count%]="&SET /a count-=1
ECHO %count% entries found
set counter=%count%
pause
#setlocal enableextensions enabledelayedexpansion
:while
if %counter% GTR 0 (
set str1 = !product[%counter%]!
echo str1
if not "x!str1:VNC=!"=="x!str1!" (
echo !product[%counter%]!
echo !uninstall[%counter%]!
set UninstallString=!uninstall[%counter%]!
echo !UninstallString!
set RunUninstall = ""
for /f "tokens=2 delims={}" %%A in ("!uninstall[%counter%]!") do echo %%A REM set RunUninstall="msiExec.exe /qn /x{%%A}" (commented for testing purposes)
call !RunUninstall!
set /a counter-=1
GOTO While
) Else (
set /a counter-=1
GOTO While
)
)
Output Snippet:
Intel(R) Network Connections 15.7.176.0
MsiExec.exe /X{26A24AE4-039D-4CA4-87B4-2F86418051F0}
MsiExec.exe /X{26A24AE4-039D-4CA4-87B4-2F86418051F0}
26A24AE4-039D-4CA4-87B4-2F86418051F0 REM set RunUninstall="msiExec.exe /qn /x{26
A24AE4-039D-4CA4-87B4-2F86418051F0}"
ECHO is off.
Microsoft .NET Framework 4.5.2
MsiExec.exe /X{26784146-6E05-3FF9-9335-786C7C0FB5BE}
MsiExec.exe /X{26784146-6E05-3FF9-9335-786C7C0FB5BE}
26784146-6E05-3FF9-9335-786C7C0FB5BE REM set RunUninstall="msiExec.exe /qn /x{26
784146-6E05-3FF9-9335-786C7C0FB5BE}"
ECHO is off.
Lenovo ThinkVantage Toolbox
MsiExec.exe /I{23170F69-40C1-2702-0920-000001000000}
MsiExec.exe /I{23170F69-40C1-2702-0920-000001000000}
23170F69-40C1-2702-0920-000001000000 REM set RunUninstall="msiExec.exe /qn /x{23
170F69-40C1-2702-0920-000001000000}"
ECHO is off.
OSFMount v1.5
MsiExec.exe /I{1B8ABA62-74F0-47ED-B18C-A43128E591B8}
MsiExec.exe /I{1B8ABA62-74F0-47ED-B18C-A43128E591B8}
1B8ABA62-74F0-47ED-B18C-A43128E591B8 REM set RunUninstall="msiExec.exe /qn /x{1B
8ABA62-74F0-47ED-B18C-A43128E591B8}"
UPDATE:
I modified the code provided by wOxxOm in an attempt to make an exception for TightVNC and uninstall it quietly. Since there is not a QuietUninstallString in the registry for TightVNC, I am attempting to accomplish this by utilizing the msiexec.exe /qn /x options. However it keeps throwing the following error:
else was unexpected at this time.
My modified version of wOxxOm's code is listed below:
#echo off
setlocal enableDelayedExpansion
for %%a in ("" "\Wow6432Node") do (
for /f "delims=" %%b in ('
reg query HKLM\SOFTWARE%%~a\Microsoft\Windows\CurrentVersion\Uninstall ^
/s /d /f "VNC" ^| findstr "HKEY_ DisplayName"
') do (
set "line=%%b"
if "!line:~0,4!"=="HKEY" (
set "key=!line!"
) else (
set Uninstall=
rem Sort /r makes QuietUninstallString the last line
for /f "tokens=2*" %%c in ('
reg query "!key!" ^| find "UninstallString" ^| sort /r
') do if not "%%d"=="" set "Uninstall=%%d"
if defined Uninstall (
for /f "tokens=2*" %%c in ("!line!") do (
set product=%%d
if "x!product:TightVNC=!"=="x!product!" (
for /f "tokens=2 delims={}" %%A in ("!Uninstall!") do (
echo Found !product!
Running msiExec.exe /qn /x{%%A} REM (COMMENTED FOR TESTING)
REM CALL msiExec.exe /qn /x{%%A}
)
) else (
echo Found %%d
echo Running !Uninstall! (COMMENTED FOR TESTING)
rem call !Uninstall!
echo.
)
)
)
)
)
Don't use spaces around = in set str1=!product[%counter%]!
Instead of if %counter% GTR 0 ( which doesn't work and makes the loop infinite)
use if %counter%==0 goto done and a :done label after the loop
With quotes you don't need to add x to the comparison.
Don't forget about 64-bit Windows and check Wow6432Node tree too (see the alternative code)
:while
if %counter%==0 goto done
set str1=!product[%counter%]!
if not "!str1:VNC=!"=="!str1!" (
..............................
rem call !RunUninstall!
)
set /a counter-=1
GOTO While
:done
Alternatively you can make the batch file much faster (the "arrays" are slow in case of many strings) by listing only the products with VNC in DisplayName first and then fetch the UninstallString:
#echo off
setlocal enableDelayedExpansion
for %%a in ("" "\Wow6432Node") do (
for /f "delims=" %%b in ('
reg query HKLM\SOFTWARE%%~a\Microsoft\Windows\CurrentVersion\Uninstall ^
/s /d /f "VNC" ^| findstr "HKEY_ DisplayName"
') do (
set "line=%%b"
if "!line:~0,4!"=="HKEY" (
set "key=!line!"
) else (
set Uninstall=
rem Sort /r makes QuietUninstallString the last line
for /f "tokens=2*" %%c in ('
reg query "!key!" ^| find "UninstallString" ^| sort /r
') do if not "%%d"=="" set "Uninstall=%%d"
if defined Uninstall (
for /f "tokens=2*" %%c in ("!line!") do echo Found %%d
echo Running !Uninstall! (COMMENTED FOR TESTING)
rem call !Uninstall!
echo.
)
)
)
)
pause
Modifying wOxxOm's code, I was able to create the following code to quietly uninstall TightVNC, UltraVNC, and RealVNC. It will also start the uninstaller for any other software with "VNC" in the DisplayName in the Windows registry under HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall and HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall.
Hopefully this is handy to someone else out there as well.
#echo off
setlocal enableDelayedExpansion
for %%a in ("" "\Wow6432Node") do (
for /f "delims=" %%b in ('
reg query HKLM\SOFTWARE%%~a\Microsoft\Windows\CurrentVersion\Uninstall ^
/s /d /f "VNC" ^| findstr "HKEY_ DisplayName"
') do (
set "line=%%b"
if "!line:~0,4!"=="HKEY" (
set "key=!line!"
) else (
set Uninstall=
rem Sort /r makes QuietUninstallString the last line
for /f "tokens=2*" %%c in ('
reg query "!key!" ^| find "UninstallString" ^| sort /r
') do if not "%%d"=="" set "Uninstall=%%d"
if defined Uninstall (
for /f "tokens=2*" %%c in ("!line!") do (
set product=%%d
if NOT "x!product:TightVNC=!"=="x!product!" (
for /f "tokens=2 delims={}" %%A in ("!Uninstall!") do (
echo Found !product!
CALL msiExec.exe /qn /x{%%A}
echo.
)
) else (
if NOT "x!product:UltraVNC=!"=="x!product!" (
for /f "tokens=1 delims=." %%A in ("!Uninstall!") do (
echo Found !product!
CALL %%A.exe" /VERYSILENT /NORESTART
echo.
)
) else (
if NOT "x!Uninstall:RealVNC=!"=="x!Uninstall!" (
for /f "tokens=1 delims=." %%A in ("!Uninstall!") do (
echo Found !product!
CALL %%A.exe" /VERYSILENT
echo.
)
) else (
echo Found %%d
CALL !Uninstall!
echo.
)
)
)
)
)
)
)
)

create a batch file to get a ip address from a network interface name

I have servers with more than 2 network interfaces.
ie. Primary.nic, BEN.nic, HB.nic etc
Using the following lines I'm getting the IP from the last NIC:
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find "IP" ^| find "Address" ^| find /v "v6"') do (
set IPAddr=%%a
)
echo=%IPAddr%
I need to find the IP address from a specific NIC name ie. "BEN"
I've tried this too:
#echo on
setlocal ENABLEEXTENSIONS
setlocal EnableDelayedExpansion
set result=false
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| "BEN.NIC" ^| find "IP Address"') do (
set IPAddr=%%a
)
echo %IPAddr%
It doesn't work.
Next script should work:
#ECHO OFF >NUL
SETLOCAL enableextensions disabledelayedexpansion
set "nicFunName=wiredEthernet"
for /F "usebackq tokens=*" %%G in (
`wmic nic where "NetConnectionID='%nicFunName%'" get index /value^|find "="`) do (
rem echo G %%G
for /F "tokens=*" %%H in ("%%G") do (
rem echo H %%H
for /F "usebackq tokens=2 delims==" %%I in (
`wmic NICCONFIG where %%H get IPAddress /value`
) do (
rem echo I %%I
for /F "tokens=1,2 delims={,}" %%J in ("%%I") do (
echo IPv4=%%J IPv6=%%K
rem or without double quotes: echo IPv4=%%~J IPv6=%%~K
)
)
)
)
ENDLOCAL
goto :eof
Where the for loops are
%%G to retrieve the NIC index in Index=0 format applicable as next wmic query where clause condition
%%H to remove a superabundand and harmful carriage return (0x0D) from wmic output
%%I to retrieve the IPAddress by index in {"<ipv4>","<ipv6>"} format
%%J to split previous output
You could add a most outer loop as follows:
for %%m in (
"Primary.nic" "BEN.nic" "HB.nic"
) do (
rem all for... code here with
rem where "NetConnectionID='%%~m'"
rem or call a subroutine or call a batch etc.
)

Using FOR Loop and SC Query in CMD Script

Suppose this instruction:
C:\>SC query state= all | find /I "adobe"
The result is:
SERVICE_NAME: AdobeARMservice
DISPLAY_NAME: Adobe Acrobat Update Service
Now I want to obtain the exact argument in this case is "AdobeARMservice"
Here my Script TestLineArgument.bat:
#echo off
cls
REM
REM for /f "delims=" %%# in ('SC query state= all ^| find /I "adobe"') do ( IS NOT WORKING
REM
set "SCQueryState=SC query state ^= all"
set "QueryAdobe=%SCQueryState%^^^| find /I ^"adobe^""
for /f "delims=" %%# in ("%QueryAdobe%") do (
set "THERSERVICE_NAME=%%#"
echo %%#
goto :END_FORSCQUERY
)
:END_FORSCQUERY
REM show the Line "SERVICE_NAME: AdobeARMservice"
echo THERSERVICE_NAME:%THERSERVICE_NAME%
FOR /f "tokens=2" %%# IN ("%THERSERVICE_NAME%") DO (
set "THESERVICE=%%#"
)
REM show the second part "AdobeARMservice"
echo THESERVICE:%THESERVICE%
My Script is not working, I don't know how use FOR with SC Command
Some clue?
#echo off
setlocal enableextensions disabledelayedexpansion
rem Initialize variables
set "serviceName="
set "displayName="
rem Execute the query and retrieve all the SERVICE_NAME lines and
rem the line with the desired display name
for /f "tokens=1,*" %%a in ('
sc query state^= all ^| findstr /i /l /c:"SERVICE_NAME" /c:"Adobe"
') do if not defined displayName if "%%a"=="SERVICE_NAME:" (
set "serviceName=%%b"
) else set "displayName=%%b"
rem Show retrieved data
if defined displayName (
echo %serviceName%
echo %displayName%
) else echo Not found
This method does not work in the manner you were trying to implement, as #MCND answer does, but if "AdobeARMservice" is only returned by sc query state when it is running, this may be useful.
It simply looks for the string "AdobeARMservice" and sets THESERVICE var to the result: If "AdobeARMservice" not found, THESERVICE remains empty. If found it gets value=AdobeARMservice
#echo off
Set "THESERVICE="
For /f %%A in ('sc query state^= all ^| find /i "AdobeARMservice"') do set THESERVICE=%%A
If "%THESERVICE%"=="" echo AdobeARMservice not found
If NOT "%THESERVICE%"=="" echo THESERVICE: %THESERVICE%
pause
My problem was simply...
#echo off
set "SERVICE=Adobe"
for /f "delims=" %%s in ('sc query state^= all ^| find /I "%SERVICE%"') do (
for /f "tokens=2" %%# in ("%%s") do (
echo THESERVICE:%%#
)
)
I tryng to avoid to change the code according to version something like : "Adobe3.2_1" or "Adobe3.2_5" or "Adobe4.5", this snippet of code only...

Using IF condition

I want to query a remote machine and if the KB22334358 is found in the systeminfo output then exit, otherwise write hostname to failed.txt.
Why doesn't this work?
systeminfo /s "remotenamemachine" | find "KB22334358"
if %errorlevel% equ 1 goto nome else goto exit
:nome
systeminfo | find "Nome host" > C:\failed.txt
:exit
exit
Seems like you might need to use the /s switch as well when you connect the next time. Also, you might want to make the find command not case sensitive by using /i. You might also want to append to the failed.txt file, like so:
systeminfo /s "remotenamemachine" | find "KB22334358"
if %errorlevel% equ 1 goto nome else goto exit
:nome
systeminfo /s "remotenamemachine" | find /i "Nome host" >> C:\failed.txt
:exit
exit
Or you can use this triple for /f loop that gets it all in one shot:
KB_Checker.bat
#echo off
setlocal ENABLEDELAYEDEXPANSION
set comp_name=%*
for /f "usebackq tokens=1* delims=" %%I in (`
systeminfo /s "%comp_name%"
`) do (
for /f "usebackq tokens=2 delims=:" %%k in (`
echo %%I ^| findstr /v "^)" ^| findstr "KB22334358"
`) do (
for /f "tokens=1* delims=" %%f in ('
echo %%k ^| findstr /v "UTC"
') do (
set temp_KB=%%f
)
)
for /f "usebackq tokens=2 delims=:" %%h in (`
echo %%I ^| findstr /i /C:"Nome host"
`) do (
for /f "tokens=1* delims=" %%f in ('
echo %%h ^| findstr /v "UTC"
') do (
set hm=%%f
)
)
) 2> nul
set KB=!temp_KB!
if not defined KB echo !hm! >> C:\failed.txt
exit
UPDATE
I updated the script above to set the comp_name environment var. You can pass the computer name into the script like so: KB_Checker.bat computer_1 and it will process that

Resources