$server = "MSSQLServer\Instance"
$srcDB = "TestDB"
$dump = "C:\Temp\EE\TestDB.sql"
# Stop on any error
$ErrorActionPreference = "stop"
[void] [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')
[void] [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SQlServer.SMOExtended')
$xfer = [Microsoft.SqlServer.Management.SMO.Transfer] ([Microsoft.SqlServer.Management.SMO.Server] $server).Databases[$srcDB]
# Set export options.
$opts = New-Object Microsoft.SqlServer.Management.SMO.ScriptingOptions
$opts.Filename = $dump
$opts.ToFileOnly = $true
$opts.AllowSystemObjects = $false
$opts.Statistics = $false
$opts.ScriptDataCompression = $false
$xfer.options = $opts
$xfer.ScriptTransfer()
It is based on scripts and explanations found at https://www.simple-talk.com/sql/database-administration/automated-script-generation-with-powershell-and-smo/. That site also shows how to get specific objects only (see "Automated scripting of objects").