Transfer Tables to a Different Schema via Powershell

Quick script to script out the transfer of tables belonging to on schema to the dbo schema.  This does no checking to see if the object exists in the dbo schema already, nor does it actually transfer the object.  It simply generates the script to do so.  I’m working on a more thorough version that will do all this, but I had a need for a quick-one off today.  This uses SQLPSX.

Import-Module SqlServer

Get-SqlServer -sqlserver 'ServerName' | %{
	Get-SqlDatabase -sqlserver $_ -dbname 'DatabaseName' | %{
		Get-SqlTable -Database $_ | where{$_.Schema -eq 'OrigSchema'} |%{
			"ALTER SCHEMA dbo TRANSFER [OrigSchema].[$($_.Name)]"

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 )

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.