Jump to content
Sign in to follow this  

Search Registry and change a specific key

Recommended Posts


Hey guys,

I'm looking to do a registry search for all ODBC connections that are connecting to a specific server that is about to be retired. I then want to point those connections to the new server.

The problem I seem to be running into is the Key may be different depending on the computer.

The String I want to change should be somewhere in this area but the exact key is unknown. \\HKEY_CURRENT_USER\Software\ODBC\ODBC.INI\[RandomKey]\

They old string is Server, REG_SZ, FTWSQL03\MSSQL3

The new string would need to be changed to FTWMSQLP19\MSSQLP19

I'm not really sure how registry search works even though I've seen a few threads referencing it.

;Search to see if the following key exists inside of an unknown key

$OldString = "Server" , "REG_SZ" , "FTWSQL03\MSSQL3")
$NewString = "Server" , "REG_SZ" , "FTWMSQLP19\MSSQLP19")

$Key1 = RegRead("HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI\[Unknown Key 1]", & $OldString
$Key2 = RegRead("HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI\[Unknown Key 2]", & $OldString

$Key1 = 1 Then
  RegWrite("HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI\[Unknown Key 1]", $NewString
$Key2 = 1 Then
  RegWrite("HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI\[Unknown Key 2]", $NewString

I'm sure I'm way off on where I'm going with this since most of this is new to me. My thought was to search all the sub-keys within HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI for a string that is Server=FTWSQL03\MSSQL3 then change it to Server=FTWMSQLP19\MSSQLP19

Share this post

Link to post
Share on other sites

There was a similar question two days ago and I suggested to have a look at a made for searching the registry.

Maybe this helps.

My UDFs and Tutorials:


Active Directory (NEW 2017-04-18 - Version - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version - Download - General Help & Support

ADO - Wiki


Share this post

Link to post
Share on other sites

perhaps something like this ...

Local $sKey, $sVal
Local $OldString = 'FTWSQL03MSSQL3'
Local $NewString = 'FTWMSQLP19MSSQLP19'

For $i = 1 To 50
    $sKey = RegEnumKey($HKCU_ODBC, $i)
    If @error <> 0 Then ExitLoop
    $sVal = RegRead($HKCU_ODBC & '' & $sKey, 'Server')
    If @error <> 0 Then ContinueLoop

    MsgBox(0, '', 'Key: ' & $sKey & @CRLF & 'Value: ' & $sVal); <-- debug (have a peek)

    If $sVal = $OldString Then
        RegWrite($HKCU_ODBC & '' & $sKey, 'Server', 'REG_SZ', $NewString)

By the way, you can't do this in AutoIt ...

$OldString = "Server" , "REG_SZ" , "FTWSQL03MSSQL3")

Commas and brackets for a function, must reside within the function.

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post

Link to post
Share on other sites

Thanks ripdad! That's exactly what I was looking for.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.