Skip to content

April 17, 2013

Find a Table with Powershell

by Scott Newman

Quick script to find which database a table resides in with powershell.  This assumes you have a file named servers.txt that lists the servers that you’ll be searching.  This uses SQLPSX as well.  Easy as cake. 

Import-Module SqlServer

$tableName = 'TableName'
$servers = Get-Content c:\Test\Servers.txt

$servers | %{
	$srv = $_
	Get-SqlDatabase -sqlserver $(Get-SqlServer -sqlserver $_ -username 'UserName' -password '') | where{-not $_.IsSystemObject} | %{
		$db = $_
		Get-SqlTable -Database $_ | where{$_.Name -eq $tableName} | %{
			write-host "$($srv) $($db.Name)"
		}
	}
}
Read more from Powershell, SQL Server

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: