Creating an ODBC connection

With Powershell 4 on Windows 8 and 2012, they’ve improve the support for ODBC connections. You just need to import the Wdac module to be able to use the different ODBC functions.

Get-Command *ODBC* shows the following functions:


For instance, if you were to create a System ODBC connection, with a 32 bit SQL driver, use the following code:

Import-Module Wdac
$OdbcDriver = Get-OdbcDriver -Name *SQL* -Platform 32-bit
If(!$OdbcDriver.Count) # Only continue if 1 SQL ODBC driver is installed
{ Add-OdbcDsn -Name "ODBC Connection Name" -DriverName $OdbcDriver.Name -Platform 32-bit -DsnType System -SetPropertyValue @("Server=SQL ServerName\SQL Server Instance", "Trusted_Connection=Yes","Database=SQL DatabaseName") }

Tags: , ,

5 responses to “Creating an ODBC connection”

  1. Jeroen Vos says :

    Yo, Mr Powershell Admin, I added a link to your site on my weblog’s usefull links section. Care to return the favor?


  2. Danilo Corrons says :

    Can I use the Add-ODBCdsn command to add a connection to a remote computer? If so, how? I can’t seem to get it to work.


  3. Powershell Administrator says :

    I’ve not ran this against a remote computer myself, but according to documentation provided by MS, you should use the -CimSession switch: -CimSession

    Runs the cmdlet in a remote session or on a remote computer. Enter a computer name or a session object, such as the output of a New-CimSession or Get-CimSession cmdlet. The default is the current session on the local computer.
    So you should be able to just enter the remote computer name after this command and if the account you’re running the PS script from has the correct access rights on the remote computer, it should create it.

    Sometimes working against a remote computer will return a different result than when you run it locally.
    So there’s also this option: create a new- PSSession to the remote computer (In this case you can also specify the account you’d want the script to be ran as, which can be very handy if you need to use another user account). Then you can use Invoke-Command to locally run Add-ODBCdsn on the remote computer through a ps remote session. Don’t forget to remove the session in the end by using Remove-PSSession. It’s a little bit more code, but it’ll run on the remote computer’s CPU and I myself believe this is a more stable way of executing code against a remote computer.
    Documentation for Invoke-Command can be found here:
    And documentation for New-PSSession (and others) can be found here:


  4. victor feinstein says :

    Would you know how to add-odbsdsn with a username/password included in the property values? i tried to look it up in regedit, looks like “lastuser” is the user name, but password is not there.. is it PWD?


    • Powershell Administrator says :

      Can you specify a little bit more? Your question isn’t completely clear.
      But, if it’s an SQL ODBC connection you’re talking about: the “Trusted_Connection=Yes” will use your logged in Windows account for authentication, but this is an ODBC SQL speciic setting, I don’t think this will work on other ODBC types. It seems that your ODBC connection uses only a username and no password, otherwise it would be listed in the registry, this is a non-recommended use as integrated security is the way to go (using the windows account and its credentials). Maybe it’s an option to change your SQL security to allow Windows Authentication. MS documentation on Add-OdbcDsn can be found here:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: