How can I create a new .reg file from the CLI

0

I'd like to create a new .reg file and add it to the registry, as suggested in this article.

REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\IniFileMapping\Autorun.inf]
@="@SYS:DoesNotExist"

It's pretty simple. But I want to script it. I thought I could just use REG ADD, but I'm not sure how to incorporate the @="@SYS:DoesNotExist" part at the end.

voices

Posted 2018-11-29T12:07:40.133

Reputation: 2 053

Answers

1

Create your batch file - e.g. fix.bat - with the following content:

echo REGEDIT4 > fix.reg
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\IniFileMapping\Autorun.inf] >> fix.reg
echo @="@SYS:DoesNotExist" >> fix.reg

Run the batch and it'll create your file with the desired content. You can manually merge the .reg file or you could go one step further and use the batch file to merge the fix.reg file created with the script into Windows Registry:

echo REGEDIT4 > fix.reg
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\IniFileMapping\Autorun.inf] >> fix.reg
echo @="@SYS:DoesNotExist" >> fix.reg
regedit.exe /S fix.reg

Kinnectus

Posted 2018-11-29T12:07:40.133

Reputation: 9 411

I appreciate it, but yuck. Is that necessary? Can't I just REG ADD it, or something like that? – voices – 2018-11-29T14:29:31.383

@tjt263 This answer provides a solution based on the way your question is worded. That is, it creates a .reg file and imports that file. – Worthwelle – 2018-11-29T16:31:06.517

what else ya got – voices – 2018-11-29T17:19:03.100

I think LotPings' answer will give you the all-in-one script you seek :) – Kinnectus – 2018-11-29T17:37:21.367

1

If you once import that key and look it up with REG QUERY,
you'll see that the first @ refers to the default key of type REG_SZ with content @SYS:DoesNotExist.

So to create the key directly with REG ADD use the following batch which as admin rights are required automatically elevates itself (with UAC dropping in):

:: Q:\Test\2018\11\29\SU_1379397.cmd
@echo off & setlocal EnableExtensions DisableDelayedExpansion
:: if not already running as admin, elevate and run batch again
net file 1>nul 2>&1 || (
  powershell -ExecutionPolicy unrestricted -Command ^
  "Start-Process -Verb RunAs -FilePath '%comspec%' -ArgumentList '/c %~f0 %*'"
  goto :eof
)
:: Put code here that needs elevation
@Echo off
Set "Key=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\IniFileMapping\Autorun.inf"
Set "Dat=@SYS:DoesNotExist"
REG ADD "%Key%" /ve /t REG_SZ /d "%Dat%" /f
TIMEOUT /T 10

LotPings

Posted 2018-11-29T12:07:40.133

Reputation: 6 150