Find database name via ODBC connection

Posted on 2011-10-12
Last Modified: 2012-05-12
I am wondering how to retreive what database is in use in ODBC connections

I know the ODBC connection name
I need to use this name to retreive the database name from ODBC in a powershell script.
I have found that i can retreive ODBC connections with the get-childitem.

But i need to use this to only retreive the database name the ODBC connection uses, and output it in a file or something similar. This is so i can detach these databases using a script afterwards.

Get-childItem -path "hklm:\software\odbc\odbc.ini\"

Open in new window

Question by:frankEQS
    LVL 5

    Expert Comment

    if you want to return it from the database itself try:

    LVL 11

    Accepted Solution

    Something like this?
    $in = Read-Host
    $pt = "HKLM:\software\ODBC\ODBC.INI\$in"
    $db = (Get-ItemProperty -Path $pt).Database
    "$in, $db" >> c:\db_list.txt

    Open in new window

    LVL 75

    Expert Comment

    by:Anthony Perkins
    >>I am wondering how to retreive what database is in use in ODBC connections<<
    Have you considered that the database name may not even be in the connection string.  It may be using the default database for that login..

    Author Comment

    we have a program, that uses ODBC to connect to IIS. in the ODBC connection we always refer to a database created by us. This database usualy is the same name as the ODBC connection, but there are deviation.

    These databases also always starts with the characters "eqs_"

    This way I should be able to get the databases out of the ODBC config file and automaticaly detach these, and move them to another server.

    Marek1712: I will try your solution soon.

    Author Comment

    Im not getting this code to work how i want.

    In the foreach loop i want to pick out each line of the $dbnavn variabel, and place it in the $db variabel.

    The code before is for getting the ODBC names out of the ODBC info which has the EQS characters in it. Ofcourse there may be a better way of doing this. ( i am a noob :P)

    anyway when i reach the foreach loop im getting a error:

    Get-ItemProperty : Cannot find path 'HKLM:\software\odbc\odbc.ini\eqs_hemit                                                          
                ' because it does not exist.
    At C:\MyScripts\finn_odbc_ini.ps1:32 char:28
    +     $db = (Get-ItemProperty <<<<  -Path "$dbnavn").Database
        + CategoryInfo          : ObjectNotFound: (HKLM:\software\...               :String) [Get-ItemProperty], ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand

    Seems like it reads the whole $dbnavn variabel, not only line by line?

    Get-childitem -path "hklm:\software\odbc\odbc.ini\" | select-object Name | out-file dbsticache.txt
    Get-Content dbsticache.txt | where { $_ -match "eqs"}  | set-Content -path dbfullsticache.txt
    Get-Content dbfullsticache.txt | Foreach-Object { $_ -replace "HKEY_LOCAL_MACHINE", "hklm:"} | Set-Content dbfullsti.txt
    # remove cache files
    remove-item dbsticache.txt
    remove-item dbfullsticache.txt
    $dbnavn = get-content dbfullsti.txt
    foreach ($dblinje in $dbnavn) {
        $db = (Get-ItemProperty -Path "$dbnavn").Database

    Open in new window


    Author Comment


    I found out i needed to trim the end of each line before trying to use get-itemproperty
    foreach ($dblinje in $dbnavn) {
        $dblinje = $dblinje.trimEnd()
        $db = (Get-ItemProperty -Path "$dblinje").Database

    Open in new window

    LVL 11

    Expert Comment

    I'm glad you've managed to solve the issue.
    I couldn't help you earlier as I don't have that much free time in my job :)

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
    The viewer will learn how to dynamically set the form action using jQuery.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now