Script Out All Indexes and Primary Keys from a Database
USE: To generate T-SQL code for all indexes and PK constraints from a database.
CLEAR $sSRCServer = "TPW520" $sSRCDatabase = "iSQLPS" $oSO = New-SqlScriptingOptions $oSO.ClusteredIndexes = $true $oSO.Indexes = $true $oSO.NonClusteredIndexes = $true $oSO.IncludeIfNotExists = $true $oTables = Get-SqlDatabase $sSRCServer $sSRCDatabase|Get-SqlTable foreach ($oTable in $oTables){ $oT = $oTable.Indexes foreach ($oIndex in $oT) { $DDL = Get-SqlScripter $oIndex -scriptingOptions $oSO "$DDL `nGO `n`n" } }