Nesting commands in windows batch file and Parsing results recursively - windows

Screenshot
I am trying to fetch all registry entries under
HKEY_Local_Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList and Delete if REG_EXPAND_SZ key contains value C:\Users\sas-
Ideal Steps Script should do :
1) Get all Registry entries for users Using :
reg query "HKEY_Local_Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
>> xyz.txt
2)If users name is sas-### (which we can get in "REG_EXPAND_SZ") then delete this entry :
for /f "skip=10 tokens=* " %%a in (xyz.txt) do (
for /f "eol=; skip=2 tokens=4 delims=\t" %k in ('reg query "%%a" /t REG_EXPAND_SZ') do (
if %k == "sas-" (reg delete "%%a")
)
)

Try this:
for /f "tokens=*" %%a in ('
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"') do (
for /f "tokens=*" %%b in ('
reg query "%%a" /v "ProfileImagePath" 2^>nul ^| find "sas-"') do
rem reg delete "%%a"
echo "%%a" >>C:\names.txt
)
I've based this on you querying the ProfileImagePath value, and deleting any of the keys that contain sas-* in the value. I've also put in a few line breaks you will have to remove (just for easy viewing on SO).
Please test this first (I've echo'd them to a text file at the moment), if you are happy with it then remove rem from the reg delete line.

You should be able to do it from a single For loop; however, I do not particularly like the idea of removing profiles using this method.
#Echo Off
For /F "Tokens=1-2*" %%A In ('Reg Query^
"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /S^
/F ProfileImagePath /V 2^>Nul') Do (
If Not "%%~A%%B"=="ProfileImagePathREG_EXPAND_SZ" (
Set "_C=Reg Delete "%%A%%B"") Else (Set "_P=%%~C"
SetLocal EnableDelayedExpansion
If Not "!_P:C:\Users\sas-=!"=="!_P!" Echo=!_C!
EndLocal))
Timeout -1
If you're happy with the console output then remove Echo= from line 8 and delete line 10.

Related

How to find registry folder by match in it`s value

I am writing a batch file to search the registry.
I need to find the folder inside HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products which ProductName key equals to "MyProduct".
I need to find this folder and delete it.
This folder I want to delete will look like this: HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products\1A0614C849C672CF0A680DCFA3921735
In this example, change MyProduct to your actual ProductName string leaving the closing doublequote untouched, on line 4:
#Echo Off
SetLocal EnableExtensions
Set "App=MyProduct"
Set "Key=HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products"
For /F "Delims=" %%G In ('^""%SystemRoot%\System32\reg.exe" Query "%Key%" /S /F
"%App%" /D /E 2^>NUL ^| "%SystemRoot%\System32\findstr.exe" /I /R /X
"%Key:\=\\%\\[^^^\\]*"^"'
) Do Echo="%SystemRoot%\System32\reg.exe" Delete "%%G" /F ^>NUL
Pause
The above will only print the deletion line that you intend to run. Once you are satisfied it is correct, to actually delete it, change the code to the following, (remembering to change your ProductName string again). Please note, that as you're deleting a key from HKEY_LOCAL_MACHINE, you will most likely need to run this script elevated, or as a user having sufficient permissions for doing so.:
#Echo Off
SetLocal EnableExtensions
Set "App=MyProduct"
Set "Key=HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products"
For /F "Delims=" %%G In ('^""%SystemRoot%\System32\reg.exe" Query "%Key%" /S /F
"%App%" /D /E 2^>NUL ^| "%SystemRoot%\System32\findstr.exe" /I /R /X
"%Key:\=\\%\\[^^^\\]*"^"'
) Do "%SystemRoot%\System32\reg.exe" Delete "%%G" /F >NUL

Excluding a character in a FOR loop

I would like to display the information regarding the ProfileImagePath value of one windows user who don't have the "_" character in his username :
#echo off
cls
for /f %%I in ('dir /a:d-h /b C:\Users\ ^| %SystemRoot%\System32\findstr.exe /b /l /v "_"') do (
FOR /F "delims=" %%k IN ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"^|findstr.exe /R "S-1-5-21-[0-9]*-[0-9]*-[0-9]*-[0-9]*$" 2^>nul') do (
reg query "%%k" /v "ProfileImagePath"|findstr /i /e /c:"%%~I"
)
)
But the findstr command also takes into account users with the "_" character, while in the first FOR command, I have excluded this character :
ProfileImagePath REG_EXPAND_SZ C:\Users\user1
ProfileImagePath REG_EXPAND_SZ C:\Users\_user1
How is it possible ? How to take this into account in the 2nd FOR command ?
findstr /i /e /c:"user1" searches for strings ending with user1. However _user1 also ends with user1 :(
Changing it to findstr /i /e /c:"\%%~I" is one way to solve your problem :)

How to set FULL computer name to a variable in windows batch file

When right clicking >Computer > Properties, my full computer name is 50 characters.
If I set Cname=%COMPUTERNAME% and echo %Cname%, the FULL name is truncated from 50 characters down to just the Host name of 14 chars
In a batch file, how is the FULL computer name extracted?
This example is based purely on the vbscript you stated was retuning the string you require:
#Echo Off
Set "Pre=HKLM\SYSTEM\CurrentControlSet"
For /F "Delims==" %%A In ('Set _ 2^>Nul') Do Set "%%A="
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Control\Computername\ActiveComputerName" /V "ComputerName" 2^>Nul
')Do Set "_ActiveComputerName=%%B"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Control\Computername\ComputerName" /V "ComputerName" 2^>Nul
')Do Set "_ComputerName=%%B"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Services\TCPIP\Parameters" /V "HostName" 2^>Nul
')Do Set "_HostName=%%B"
Set _
Simply choose the example which outputs the string you were requiring for your purposes.
Either:
#Echo Off
Set "Pre=HKLM\SYSTEM\CurrentControlSet"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Control\Computername\ActiveComputerName" /V "ComputerName" 2^>Nul
')Do Set "ActiveComputerName=%%B"
Set ActiveComputerName
Pause
Else:
#Echo Off
Set "Pre=HKLM\SYSTEM\CurrentControlSet"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Control\Computername\ComputerName" /V "ComputerName" 2^>Nul
')Do Set "Computer-Name=%%B"
Set Computer-Name
Pause
Or:
#Echo Off
Set "Pre=HKLM\SYSTEM\CurrentControlSet"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Services\TCPIP\Parameters" /V "HostName" 2^>Nul
')Do Set "Host-Name=%%B"
Set Host-Name
Pause
For the reference for Computer Name and Full computer name
https://superuser.com/questions/640046/what-is-the-difference-between-computer-name-and-full-computer-name
Now coming to your question, You can get your computer name with following ways:
%computername%
net config workstation | findstr /C:"Full Computer name"
wmic computersystem get name
Reference :
https://www.windows-commandline.com/find-computer-name-from-command-line/
There is no limitation of DOS console, I have tested as below:

Use reg_query to return data only for Typed URL and SystemshutDown

C:\Users\Admin1>REG QUERY HKLM\SYSTEM\ControlSet001\Control\Windows /v ShutdownTime
Retruns
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Windows
ShutdownTime REG_BINARY 42B96F5BC9F7D101
I want CMD to show only the data of the key 42B96F5BC9F7D101 so that I can output this to a csv file
Ive googled and read the forum and obtained this from another example -
for /f "tokens=2*" %%a in ('REG QUERY HKLM\SYSTEM\ControlSet001\Control\Windows /v ShutdownTime 2^>nul') do set "ShutdownTime=%%b"
echo %ShutdownTime%
which returns
C:\Users\Admin1>for /f "tokens=2*" %%a in ('REG QUERY HKLM\SYSTEM\ControlSet001
Control\Windows /v ShutdownTime 2^>nul') do set "ShutdownTime=%%b"
%%a was unexpected at this time.
C:\Users\Admin1>echo %ShutdownTime%
%ShutdownTime%
I also want to obtain only the data to display from
REG QUERY HKCU\Software\Microsoft\Internet Explorer\TypedURLs /v url1
Any help greatly appreciated
%%a was unexpected at this time.
That error is because for loop parameters are named with a single % when used in a cmd shell and with a double %% when used within a batch file:
%%parameter : A replaceable parameter:
in a batch file use %%G (on the command line %G)
Source for /f
So you need to replace %%a with %a and %%b with %b.
Example 1:
F:\test>#for /f "tokens=2*" %a in ('REG QUERY HKLM\SYSTEM\ControlSet001\Control\Windows /v ShutdownTime 2^>nul') do #set "ShutdownTime=%b"
F:\test>#echo %ShutdownTime%
BE7AE7FCE7DCD101
Example 2:
F:\test>#for /f "tokens=2*" %a in ('REG QUERY "HKCU\Software\Microsoft\Internet Explorer\TypedURLs" /v url1 2^>nul') do #set "Url1=%b"
F:\test>#echo %Url1%
http://www.example.com
Note:
Example 2 has quotes " around the key name as it contains spaces.
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
for /f - Loop command against the results of another command.

Path over registry (windows)

I try to get a path over the regestry of windows. My Problem now is how do I get the Path out of MATLAB_ROOT_32?
for /F "tokens=* delims='C'" %%i IN ('reg query HKLM\SOFTWARE\WOW6432NODE\Mathworks\Matlab\7.9.1 /v MATLABROOT') do (set MATLAB_ROOT_32=%%i)
echo %MATLAB_ROOT_32%
set i=
rem GOTO Split1
rem :Split1
REM -- Split the result into MATLABROOT, REG_SZ and Folder using space as delimiter
for /f "tokens=1,2,3 delims='C'" %%a in ("%MATLAB_ROOT_32%") do set useless1=%%a&set useless2=%%b&set MATLAB_x32=%%c
echo %Matlab_x32%
The plan is to get the MATLAB Path in the Matlab_x32 variable.
This works for me:
#echo off
setlocal ENABLEEXTENSIONS
set MATLAB_VERSION=8.3
set KEY_NAME=HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432NODE\MathWorks\MATLAB\%MATLAB_VERSION%
set VALUE_NAME=MATLABROOT
for /F "usebackq tokens=2*" %%A IN (`reg query "%KEY_NAME%" /v "%VALUE_NAME%" 2^>nul ^| find "%VALUE_NAME%"`) do (
set MATLABROOT=%%B
)
echo %MATLABROOT%
Just change the Matlab version to whatever you're using and it should be fine. There are variations in the output from reg depending on the OS version, but this should cope (I think!)

Resources