Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8718805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:38:12+00:00 2026-06-13T06:38:12+00:00

For my job I often have to script out a table with all its

  • 0

For my job I often have to script out a table with all its keys, constraints and Triggers (basically a full script to recreate the table) from a Microsoft SQL 2008 server.I also have to do this for procedures and triggers.
What I do now is open SSMS right click the object and select script to and select to script it to a file. So if I have 3 procedures to do and 10 tables and 1 trigger I end up doing this 14 times .
What I would like is a powershell script that I could feed a list of objects to and then it would go and use SMO to script each on out to an individual file.

Thanks for the help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:38:13+00:00Added an answer on June 13, 2026 at 6:38 am

    Here is a PowerShell function I use whenever I have to script a database. It should be easy to modify just to scripts the objects you need.

    function SQL-Script-Database
    {
        <#
        .SYNOPSIS
        Script all database objects for the given database.
    
        .DESCRIPTION
        This  function scripts all database objects  (i.e.: tables,  views, stored
        procedures,  and user defined functions) for the specified database on the
        the given server\instance. It creates a subdirectory per object type under 
        the path specified.
    
        .PARAMETER savePath
        The root path where to save object definitions.
    
        .PARAMETER database
        The database to script (default = $global:DatabaseName)
    
        .PARAMETER DatabaseServer 
        The database server to be used (default: $global:DatabaseServer).
    
        .PARAMETER InstanceName 
        The instance name to be used (default: $global:InstanceName).
    
        .EXAMPLE
        SQL-Script-Database c:\temp AOIDB
        #>
    
        param (
            [parameter(Mandatory = $true)][string] $savePath,
            [parameter(Mandatory = $false)][string] $database = $global:DatabaseName,
            [parameter(Mandatory = $false)][string] $DatabaseServer = $global:DatabaseServer,
            [parameter(Mandatory = $false)][string] $InstanceName = $global:InstanceName
        )
    
        try
        {
            if (!$DatabaseServer -or !$InstanceName)
                { throw "`$DatabaseServer or `$InstanceName variable is not properly initialized" }
    
            $ServerInstance = SQL-Get-Server-Instance $DatabaseServer $InstanceName
    
            [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
    
            $s = New-Object Microsoft.SqlServer.Management.Smo.Server($ServerInstance)
            $db = $s.databases[$database]
    
            $objects = $db.Tables
            $objects += $db.Views
            $objects += $db.StoredProcedures
            $objects += $db.UserDefinedFunctions
    
            $scripter = New-Object ('Microsoft.SqlServer.Management.Smo.Scripter') ($s)
    
            $scripter.Options.AnsiFile = $true
            $scripter.Options.IncludeHeaders = $false
            $scripter.Options.ScriptOwner = $false
            $scripter.Options.AppendToFile = $false
            $scripter.Options.AllowSystemobjects = $false
            $scripter.Options.ScriptDrops = $false
            $scripter.Options.WithDependencies = $false
            $scripter.Options.SchemaQualify = $false
            $scripter.Options.SchemaQualifyForeignKeysReferences = $false
            $scripter.Options.ScriptBatchTerminator = $false
    
            $scripter.Options.Indexes = $true
            $scripter.Options.ClusteredIndexes = $true
            $scripter.Options.NonClusteredIndexes = $true
            $scripter.Options.NoCollation = $true
    
            $scripter.Options.DriAll = $true
            $scripter.Options.DriIncludeSystemNames = $false
    
            $scripter.Options.ToFileOnly = $true
            $scripter.Options.Permissions = $true
    
            foreach ($o in $objects | where {!($_.IsSystemObject)}) 
            {
                $typeFolder=$o.GetType().Name 
    
                if (!(Test-Path -Path "$savepath\$typeFolder")) 
                    { New-Item -Type Directory -name "$typeFolder"-path "$savePath" | Out-Null }
    
                $file = $o -replace "\[|\]"
                $file = $file.Replace("dbo.", "")
    
                $scripter.Options.FileName = "$savePath\$typeFolder\$file.sql"
                $scripter.Script($o)
            }
        }
    
        catch
        {
            Util-Log-Error "`t`t$($MyInvocation.InvocationName): $_"
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a job list. Each job has its own run time. They need
I have this HTML string which often has a lot of whitespaces Example: <p>All
Background We have worker processes that read jobs from a table in SqlServer then
ladies and gentlemen! Very often on my job I meet the following requirement from
I have a job interview tomorrow and I'm trying to answer this question: There
I have a job that must execute each monday. This job run in a
I have a job stored on a database, scheduled to run every day. But
How often is language the deciding factor for selecting a job? Many of us
I often use the refactor -> rename functionality in eclipse and I also have
I have a typedef enum I use to represent a state of a job

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.