I need a batch script to create an ODBC/DSN connection from a .bat file in Windows. How can I do this?

  • .bat only gives you access to what is at command line, so I don't think it's possible. It should be possible with visual basic though, although I've never done it. – Robert Sep 25 '12 at 23:04
  • @Robert That's possible. Already did. – Jhonathan Sep 26 '12 at 14:06


Manually create an ODBC connection on your PC using the "Data Sources (ODBC)" applet in Admin tools.

Launch regedit and go to HKLM\Software\ODBC and export the whole lot from ODBC down to a .reg file. This is a right click option on the folder.

Open the exported file in Notepad now you need to delete everything that doesn't refer to your particular ODBC connection.

You should end up with a registry file looking something like this

Windows Registry Editor Version 5.00

"TEST"="SQL Server"


It may be slightly different depending on connection settings and such like. You will need the two sections, the data sources defines the type of connection, in this instance a SQL server one, then the bit that tells you what it is called, which driver to use and what sort of authentication to use. this will all be done when you set up the connection, you just need the .reg file with it in.

You can then import it on any pc by either double clicking the reg file or if you have the facility run a script to import it using regedit. You can call this import from a batch file:

regedit /s FileToImport.reg
  • 4
    Be careful: there are two sets of registry keys for odbc connections on 64bit machines. – Joel Coel Sep 4 '12 at 18:44
  • The registry entries can also be created with reg.exe. If you're going to move a file around anyway, it might be better to create a file DSN instead of a system DSN. – Ansgar Wiechers Sep 4 '12 at 20:12
  • works! I opened ran regedit and exported the current dsn to make a copy to install on other machines. – crh225 Jan 15 '13 at 19:05
  • 5
    Note for 32-bit programs running on 64-bit systems the path is HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ODBC\.... – Nux May 6 '14 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.