Search code examples
batch-filecmdregistry

Get Errorcode of Reg Query in one Line


i want to get either 0 or 1 from the following reg query: reg query "hklm\Software\Microsoft\Windows\Currentversion\WindowsUpdate\Auto Update\Rebootrequired"

Instead of getting the Updates or the Error Message, i want to output the Errorcode.

The Problem is, that the whole command must be put in one Line!

Something like "reg query.... 2>&1 | echo %Errorlevel%

Thanks!

Sorry for my bad English!


Solution

  • If you need to do it all on one line then you will need to force delayed expansion to be enabled so that you can echo the errorlevel correctly.

    cmd /V:on /C "reg query "hklm\Software\Microsoft\Windows\Currentversion\WindowsUpdate\Auto Update\Rebootrequired" >nul 2>&1 &echo !errorlevel!"
    

    You can also use this.

    reg query "hklm\Software\Microsoft\Windows\Currentversion\WindowsUpdate\Auto Update\Rebootrequired" >nul 2>&1 &CALL echo %^errorlevel%