Hey, Scripting Guy! How Can I Tell Which Tables and Columns Are in a Database Without Opening It?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there a way to figure out which tables and columns are in a database without having to open it up?

– DT

Spacer Hey, Scripting Guy! Answer

Hi DT,

The Impressionists are back in Chicago! For the past six months, they were hibernating down in Ft. Worth, Texas, in the United States. But now they are back. In true Ferris Bueller style, I am standing here looking at Cezanne’s The Vase of Tulips and am impressed by the way the flowers seem at ease with their surroundings, are at one with their vase, and enjoy a brief repose with the fruit on the table (of course the fruit is innocent—it was framed; we’ll be here all week, try the veal). It is the structure of the painting that creates an organic entity from myriad brush strokes. It is the schema that impresses me.

To become one with your database, you will also want to enjoy the schema of the database. While waiting for a pizza, we knocked out the GetDataBaseSchema.ps1 script.

This week we will be talking about scripting Microsoft Office Access. The Databases hub is a great jumping-off point because it highlights the available Office Access scripts we have in the TechNet Script Center. There are also links to the Office Access scripts we have in the Script Center Script Repository and on the Community-Submitted Scripts Center. When taken as a whole, there are tons of examples for you to review. For information about downloading and installing Windows PowerShell, you can go to the Windows PowerShell Scripting Hub.

The The GetDataBaseSchema.ps1 script is seen here.

Ok, so the GetDataBaseShema.ps1 script is rather long. But we have already talked about several of the functions recently:

Check-Path function

New-Line function

The Get-DataType function accepts a single parameter—a number that represents a data type enumeration value. Inside the function, it uses the Parse static method from the System.Enum .NET Framework class. The Parse method takes two parameters: the first is the name of the enumeration class, and the second is the value of the enumeration. If we were to call this from within the Windows PowerShell console, it might look like this:

As you can see, it takes a number and turns it into a string, so the output is more intelligible. We will use this when we display the data types contained in each column of the database table. The function is seen here:

Now we come to the main function we will be creating today. The Get-DataBaseSchema function accepts a path to the database file as seen here:

We then call the New-Line function to print out the name of the database with an underline:

Now we specify the provider and the data source, create the connection to the database, and open the connection. This is seen here:

We retrieve the database schema by using the GetSchema method. We are interested in tables, and therefore limit the returned information to tables by supplying the “Tables” keyword to the method call. We then take the collection of table schema objects and pipeline it to the ForEach-Object cmdlet, which allows us to work through the collection of objects. We use the $_ variable to refer to a single object on the pipeline. We look at the table_type property. If it is equal to TABLE, we know it was a user-created table and not a system table. We then sent the table name back to the $Tables variable. This section of code is seen here:

Well, that was as much fun as extra mushrooms and sausage on a Chicago deep-dish pizza. Because it is possible the database could have more than user-created table in the database, we use the ForEach statement to walk through the array of table names stored in the $Tables variable. If there is only one table, we do not need to do anything because Windows PowerShell does not generate an error the way VBScript would. We then print out the name of the table and underline it by using the New-Line function. This is seen here:

The next thing we need to do is call the GetSchema method and tell it we are interested in the columns. We then pipeline the columns to the ForEach-Object cmdlet and look for columns that have the same table name that is stored in the $table variable. After we have retrieved the columns for the particular table, we print out the column name and send the data_type value to the Get-DataType function. We talked about this earlier. This portion of the function is seen here:

The last thing we need to do is close the connection to the database. This is important to free resources and to minimize database locking issues. To close the database, we use the Close method from the connection object as seen here:

The entry point to the script takes the path to the database, and then calls the Check-Path and Get-DataBaseSchema functions as seen here:

When we run the script, we are given the output seen here:

Image of the script's output


Well, DT, we have come to the conclusion of another script. I predict you will be able to use the ability to write to an Access Database often. We will see you tomorrow for another really cool installment of “Hey, Scripting Guy!” as Office Access Database Week continues. Until then, keep on scripting (thanks CA)!


Ed Wilson and Craig Liebendorfer, Scripting Guys


No Comment.