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