Powershell Script to Script Create & Drop of Foreign Key Constraints

Not much to more to add after that title.  Use at your own risk.

cls
$server = "ServerName"

$tables = @("Table1", "Table2", "Table3", "Table4", "Table5")
$index_drop_script = "C:\Test\FKDrops.sql"
$index_create_script = "C:\Test\FKCreates.sql"

$database = Get-SqlDatabase -dbname "DatabaseName" -sqlserver $server
$tables = $database.Tables | where{$tables -contains $_.Name}
foreach($table in $tables)
{
	foreach($fk in $table.ForeignKeys)
	{
		#script create FK's
		$scriptingCreateOptions = New-Object Microsoft.SqlServer.Management.Smo.ScriptingOptions
		$scriptingCreateOptions.IncludeDatabaseContext = $false
		$scriptingCreateOptions.IncludeHeaders = $false
		$scriptingCreateOptions.IncludeIfNotExists = $true
		$scriptingCreateOptions.DriForeignKeys = $true
		$fk.Script($scriptingCreateOptions) -join "`nGO`n`n" | Out-File -FilePath $index_create_script -Append
		
		#script drop FK's
		$scriptingDropOptions = New-Object Microsoft.SqlServer.Management.Smo.ScriptingOptions
		$scriptingDropOptions.IncludeIfNotExists = $true
		$scriptingDropOptions.IncludeHeaders = $false
		$scriptingDropOptions.ScriptDrops = $true
		$fk.Script($scriptingDropOptions) -join "`nGO`n`n" | Out-File -FilePath $index_drop_script -Append
		
	}
}


Get Sql Server File Free Space

Quick script to get the free space in your data files in sql server.  Not really tested or anything.  Use at your own risk.

cls
$server = Get-SqlServer -sqlserver "ServerName"

$outputs = @();

foreach($db in $server.Databases | where{!($_.IsSystemObject)})
{
	foreach($fileGroup in $db.FileGroups)
	{
		foreach($dataFile in $fileGroup.Files)
		{
			$space = ($dataFile.AvailableSpace * 1024) 
			$output = New-Object -TypeName PSObject -Property @{
				DBName = $db.Name
			    FileName = $dataFile.FileName
				Type = "Data File"
			    FreeSpaceGB = ($space / 1GB)
			}
			$outputs += $output;
		}
	}
	foreach($logFile in $db.LogFiles)
	{
		$space = (($logFile.Size - $logFile.SpaceUsed) * 1024)
		$output = New-Object -TypeName PSObject -Property @{
			DBName = $db.Name
		    FileName = $logFile.FileName
			Type = "Log File"
		    FreeSpaceGB = ($space / 1GB)
		}
		$outputs += $output;
	}
}

$outputs | SELECT DBName, FileName, Type, FreeSpaceGB | Format-Table -AutoSize