Sql-server – Powershell to dump database schema to text file

dumppowershellschemasql server

I want to be able to dump a database schema to a sql text file using Powershell. I want this because our DBAs want to be able to do a diff on the file that is committed to SVN. They won't execute it if they can't see what it contains.

I've found many scripts that backup to a binary .bak file, but I can't find any that dump as a sql text file.

How do you do this in Powershell?

Best Answer

Use the dbatools module.

From your svn you need to build your solution. With those dacpacs you can use the following command.

Publish-DbaDacPackage -SqlInstance sql2017 -Database WideWorldImporters -Path C:\temp\sql2016-WideWorldImporters.dacpac -PublishXml C:\temp\sql2016-WideWorldImporters-publish.xml -GenerateDeploymentReport -ScriptOnly

Reference: https://docs.dbatools.io/#Publish-DbaDacPackage