Find a Table with Powershell

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)"
		}
	}
}

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 )

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.