Generate sqlserver table creation scripts from Powershell

NPG Ax143583; Edith Olivier by Lady Ottoline Morrell
I didn’t know you could do this – the ‘table’ object in the SQL provider has a ‘script’ method, so you can do the following:

$ServerInstance = "whatever"
$DatabaseName = "whatever"
foreach ($TableName in dir Sqlserver:sql$ServerInstancedatabases$DatabaseNametables) {

It’s a rough equivalent to doing a right click >> ‘Script table as’ >> ‘Create’ in SSMS, except you can do it for some or all of the tables on a database.

I’m looking forward to playing with the ‘script’ method for other database objects.

Pic: Edith Olivier and the Palladian Bridge in the grounds of Wilton House, by Lady Ottoline Morrell [Public domain], via Wikimedia Commons