Opening Databases from PowerShell

The easiest way of accessing databases right from PowerShell is to visit control panel and open the Data Sources (ODBC) module (which resides in Administrative Tools inside control panel). Use the GUI to set up the database type by clicking the "User DN" or "System DN" tab and then click Add. Remember to write down the "Data Source name" you assign because that’s the only thing PowerShell is going to need.

You should next use this code (replace "myDataSource" with the data source name you assigned, and replace TableName with the table name inside your database you want to display):

$objConnection = New-Object -comobject ADODB.Connection
$objRS = $objConnection.Execute("SELECT * FROM TableName")
while ($objRS.EOF -ne $True) {
foreach ($field in $objRS.Fields) {
'{0,30} = {1,-30}' -f $, $field.value



About Cameron
I'm a final year Computer Science/Information Systems major. Already finished my BA in Politics/Philosophy. I do web and software freelance on the side, while I finish studying. Hoping to be self-employed by the end of my degree, otherwise off into the real-world I go....

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: