{"id":256,"date":"2008-02-07T13:23:57","date_gmt":"2008-02-07T21:23:57","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2008\/02\/07\/registry-queries-the-easy-way\/"},"modified":"2008-02-11T15:17:45","modified_gmt":"2008-02-11T23:17:45","slug":"registry-queries-the-easy-way","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/02\/07\/registry-queries-the-easy-way\/","title":{"rendered":"Registry queries the easy way"},"content":{"rendered":"<p>Another topic that I&rsquo;ve seen resurface in the forums I frequent is reading remote registries. If you know exactly what you&rsquo;re looking for it doesn&rsquo;t get any easier than using REG.EXE from the command line.  Here are some examples from the command&rsquo;s help:<\/p>\n<p>Examples:<\/p>\n<p>REG QUERY HKLM\\Software\\Microsoft\\ResKit \/v Version<br \/>\nDisplays the value of the registry value Version<\/p>\n<p>REG QUERY \\ABC\\HKLM\\Software\\Microsoft\\ResKit\\Nt\\Setup \/s<br \/>\nDisplays all subkeys and values under the registry key Setup<br \/>\non remote machine ABC<\/p>\n<p>REG QUERY HKLM\\Software\\Microsoft\\ResKit\\Nt\\Setup \/se #<br \/>\nDisplays all the subkeys and values with &ldquo;#&rdquo; as the seperator<br \/>\nfor all valuenames whose type is REG_MULTI_SZ.<\/p>\n<p>REG QUERY HKLM \/f SYSTEM \/t REG_SZ \/c \/e<br \/>\nDisplays Key, Value and Data with case sensitive and exact<br \/>\noccurrences of &ldquo;SYSTEM&rdquo; under HKLM root for the data type REG_SZ<\/p>\n<p>REG QUERY HKCU \/f 0F \/d \/t REG_BINARY<br \/>\nDisplays Key, Value and Data for the occurrences of &ldquo;0F&rdquo; in data<br \/>\nunder HKCU root for the data type REG_BINARY<\/p>\n<p>REG QUERY HKLM\\SOFTWARE \/ve<br \/>\nDisplays Value and Data for the empty value (Default)<br \/>\nunder HKLM\\SOFTWARE<\/p>\n<p>To see the complete help open a command prompt and type REG query \/? or REG \/? to see all the things this tool can accomplish.<\/p>\n<p>I put together a batch file to streamline the query process. It is also attached as a text file.<\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">@echo off<br \/>\nREM ReqQuery.bat<br \/>\nREM Keywords: Registry,REG,REMote<br \/>\nREM USAGE <br \/>\nREM RegQuery.bat [computername] <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">::**********NOTES**********<br \/>\nREM This script will the registry for the specified key. <br \/>\nREM The computer name and registry key value will be displayed. <br \/>\nREM You can specify a computername as a runtime parameter. <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">REM If you don&rsquo;t specify a name, the script will query <br \/>\nREM the local machine. <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">REM If you want to save results run<br \/>\nREM RegQueryList.bat &gt; results.txt <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">REM To process a list of computers use an expression like this:<br \/>\nREM   for \/f %s in (servers.txt) do @regquery.bat %s &gt;&gt; results.txt <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">REM If you don&rsquo;t specify a computer, the local computer will<br \/>\nREM be queried. <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">::**********CONFIGURATION**********<br \/>\nREM Define the registry path to query. Do not use quotes. <br \/>\nREM Remote computers can query either HKLM or HKCU, although as<br \/>\nREM a practical matter you can really only use HKLM <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">  set regPath=hklm\\software\\microsoft\\windows NT\\currentversion <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">REM Enter the registry key that you want the value of:<br \/>\nset regKey=RegisteredOwner <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">::**********MAIN SCRIPT**********<br \/>\nif %1$==$ (<br \/>\nset computer=%computername%<br \/>\n) else (<br \/>\nset computer=%1<br \/>\n) <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">REM uncomment next line for debugging<br \/>\nREM echo Reg Query &ldquo;\\%computer%\\%regpath%&rdquo; \/v %regkey% <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">FOR \/F &ldquo;tokens=*&rdquo; %%a in (&lsquo;Reg Query &ldquo;\\%computer%\\%regpath%&rdquo; \/v %regkey% ^| <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">find \/i &ldquo;%regkey%&rdquo;&rsquo;) do @echo %computer%  %%a <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">GOTO :OUT <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">:OUT<br \/>\nset regPath=<br \/>\nset regKey=<br \/>\nset computer= <\/font><\/p>\n<p><font face=\"Courier New\" color=\"#008040\" style=\"color: rgb(0, 128, 64);\">:EOF<\/font><\/p>\n<p>The script takes a computer name as a run time parameter. If you don&rsquo;t specify one then the local computer will be queried.<\/p>\n<p>The script is intended to return the value of a single registry key. There are two variables you need to edit in the script before running it. Of course, you may prefer to modify this script so you can pass keys and values as additional run time parameters.<\/p>\n<p>To query a single computer, open a command prompt in your script directory and run something like this:<\/p>\n<p><font face=\"Courier New\">C:\\Scripts&gt;regquery jdhit-dc01<br \/>\njdhit-dc01  RegisteredOwner    REG_SZ    Jeffery D. Hicks<\/font><\/p>\n<p>As written you&rsquo;ll need to use traditional console redirection to save the results:<\/p>\n<p><font face=\"Courier New\">C:\\Scripts&gt;regquery jdhit-dc01 <\/font>&gt; owner.txt<\/p>\n<p>What about a bunch of computers?  Easy. Use the FOR command like this:<\/p>\n<p><font face=\"Courier New\">c:\\Scripts\\for \/f %s in (servers.txt) do @regquery.bat %s &gt;&gt; owners.txt<\/font><\/p>\n<p>If owners.txt already exists all output will be appended to it so you might need to delete first if it exists.<\/p>\n<p>There&rsquo;s no provision for alternate credentials using REG, so you&rsquo;ll likely need admin rights for the remote computer you are querying.<\/p>\n<p>If you have simple needs, a simple tool like REG will do the trick.  Next time we&rsquo;ll revisit this topic with VBScript and WMI.<\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:0767317B-992E-4b12-91E0-4F059A8CECA8:222bf1e7-84c9-48af-a5a9-28edb3dd8be7\" style=\"margin: 0px; padding: 0px; display: inline;\"><span class=\"sizeLess20\">Technorati Tags: <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/REG\"><span class=\"sizeLess20\">REG<\/span><\/a><span class=\"sizeLess20\">, <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/Registry\"><span class=\"sizeLess20\">Registry<\/span><\/a><span class=\"sizeLess20\">, <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/CMD\"><span class=\"sizeLess20\">CMD<\/span><\/a><span class=\"sizeLess20\">, <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/Batch\"><span class=\"sizeLess20\">Batch<\/span><\/a><span class=\"sizeLess20\">, <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/Scripting\"><span class=\"sizeLess20\">Scripting<\/span><\/a><span class=\"sizeLess20\">, <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/Automation\"><span class=\"sizeLess20\">Automation<\/span><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Another topic that I&#8217;ve seen resurface in the forums I frequent is reading remote registries. If you know exactly what you&#8217;re looking for it doesn&#8217;t get any easier than using REG.EXE from the command line. Here&#8217;s how to use it and a batch file ready for you to use.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[18],"tags":[32,55,33,56,34,31],"class_list":["post-256","post","type-post","status-publish","format-standard","hentry","category-batchcmd","tag-batch","tag-cli","tag-cmd","tag-command-line","tag-regexe","tag-registry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=256"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/256\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}