{"id":16611,"date":"2010-11-04T00:01:00","date_gmt":"2010-11-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/11\/04\/use-powershell-to-script-sql-database-objects\/"},"modified":"2010-11-04T00:01:00","modified_gmt":"2010-11-04T00:01:00","slug":"use-powershell-to-script-sql-database-objects","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-script-sql-database-objects\/","title":{"rendered":"Use PowerShell to Script SQL Database Objects"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><b>Summary:<\/b> Learn how to use Windows PowerShell to automate creation of SQL Database objects such as tables, stored procedures, and functions.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/>Hey, Scripting Guy! How can I use Windows PowerShell to script out SQL Server database objects?<\/p>\n<p>&#8212; AA<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/>Hello AA, Microsoft Scripting Guy Ed Wilson here. Today we have one final blog post from Aaron Nelson before he heads off to <a href=\"http:\/\/www.sqlpass.org\/summit\/na2010\/\">SQL PASS<\/a> summit 2010. <\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8117.HSG-11-4-10-01.jpg\" border=\"0\" \/><\/p>\n<p>Aaron Nelson is a Senior SQL Server Architect with over 10 years of experience in architecture, business intelligence, development, and performance tuning of SQL Server. He has experience managing enterprise-wide data needs in both transactional and data warehouse environments. Aaron holds certifications for MCITP Business Intelligence Developer, Database Administrator, Database Developer; and MCTS: Windows Server Virtualization, Configuration (meaning Hyper-V). Aaron Nelson ( @SQLvariant ) <a href=\"http:\/\/sqlvariant.com\/wordpress\/\">SQLvariant.com\/wordpress\/<\/a><\/p>\n<p>Aaron helps run the <a href=\"http:\/\/appdev.sqlpass.org\/\"><b>AppDev<\/b><\/a> and <a href=\"http:\/\/powershell.sqlpass.org\/\"><b>PowerShell<\/b><\/a> Virtual Chapters of PASS, volunteers for the local PASS Chapter <a href=\"http:\/\/www.atlantamdf.com\/\"><b>AtlantaMDF<\/b><\/a>, co-runs the Atlanta <a href=\"http:\/\/powershellgroup.org\/atlanta.ga\"><b>PowerShell<\/b><\/a> User Group, and&nbsp;help organize <a href=\"http:\/\/sqlsaturday.com\/41\/eventhome.aspx\"><b>SQL Saturday<\/b><\/a> in Atlanta. Again, here&#8217;s Aaron. <\/p>\n<p>&nbsp;<\/p>\n<h2>Scripting SQL Server Databases with Windows PowerShell<\/h2>\n<p>Speaking of train stations, did you know that when you land at <a href=\"http:\/\/www.atlanta-airport.com\/\">Atlanta Hartsfield-Jackson International Airport<\/a> you can take a MARTA train straight to the heart of downtown Atlanta? The&nbsp;<a href=\"http:\/\/www.gwcc.com\/\">Georgia World Congress Center<\/a> is just <a href=\"http:\/\/itsmarta.com\/ew-omn-overview.aspx\">one stop away<\/a> from Five Points station and that is where <a href=\"http:\/\/northamerica.msteched.com\/?mtag=sqlvariant\">Tech&middot;Ed<\/a> is being held in 2011. On behalf of the <a href=\"http:\/\/powershellgroup.org\/atlanta.ga\">Atlanta PowerShell User Group<\/a> we sure hope that you can make it out here next year J&nbsp; &nbsp;Dr Scripto can be seen in the following image checking it out. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4745.HSG-11-4-10-02.jpg\" border=\"0\" \/>&nbsp;<\/p>\n<p>A vital task in SQL Server (or just about any RDBMS for that matter) is being able to script out database objects (tables, stored procedures, functions, and more) so that you can check them into source control, create deployment packages, create a new database from tables in an existing database (think data warehouse), compare between Prod and Dev. These are just some reasons people around the world have for scripting out portions or even all of their databases but in the decade or more that I have been working with databases these are some of the most prevalent reasons.<\/p>\n<p>Today I want to share with you some quirks and one outright pitfall that I ran into when trying to determine how to best script out databases with Windows PowerShell to achieve different goals. Before I go on though I want to make sure that you know that we, as SQL Server DBAs, have never had an easier time of scripting out databases than we do today with Windows PowerShell. It is just, well; I am one that is fine with learning the hard way as long as we are accomplishing tasks along the way. Therefore, I want to save you the trouble of a few lessons I learned the old-fashioned way and here&#8217;s number one.<\/p>\n<p>&nbsp;<\/p>\n<h2>Simple Scripting From the Provider<\/h2>\n<p>I was so excited when I first discovered how easy it is to script out objects by using Windows PowerShell that I got a little ahead of myself. You see it is easy to script out just about anything in SQL Server but like the saying goes &#8220;the devil is in the details&#8221;. Just look:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">cd SQLSERVER:\\sql\\WIN7NetBook\\Default\\Databases\\ADVENTUREWORKS\\TABLES<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$PTH = get-item Production.TransactionHistory<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$PTH.Script()<\/span><\/p>\n<p>That was so easy wasn&#8217;t it? I mean, as long as you remembered to load those snap-ins from the other day. That is pretty cool but on closer examination a SQL person will start to ask: Where&#8217;s the Primary Key? Where are the Indexes? What about scripting Foreign Keys? What if it already exists, how do I make it generate the <b>Drop<\/b> statement? It just so happens that I have a funny story about that. I was so excited that I could script out objects and just wanted to understand how to generate those <b>DROP<\/b> statements. So I grabbed my variable and piped it to <b>Get-Member<\/b> like you are supposed to do <strong>( $PTH | Get-Member )<\/strong> and when I saw that it spit out a method called &#8220;<b>Drop<\/b>&#8221; I wanted to shout hooray! Except, being caught up in the moment, I did not stop to think what I was looking at and so guess what I did: <span style=\"font-family: courier new,courier\">$PTH.Drop()<\/span> Turns out, that does not &#8216;Script the Drop&#8217; it &#8216;Does the Drop&#8217; so be careful with that one.<\/p>\n<p>You can even script out all tables from right here with a fairly small amount of code:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">foreach ($tbl in Get-ChildItem SQLSERVER:\\SQL\\Win7NetBook\\Default\\DATABASES\\ADVENTUREWORKS\\TABLES ) <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">{<br \/>$k=&#8221;C:\\Temp\\&#8221; + $($tbl.Schema) + &#8220;.&#8221; + $($tbl.name) + &#8220;_table.SQL&#8221;<br \/>$tbl.Script() &gt; $k<br \/>}<\/span><\/p>\n<p>The only drawback to the approach that was discussed earlier is that we (Data Professionals)&nbsp;tend to want some more fine-grained control over scripting out all the objects in our databases. There are lots of options available to you when you scripting out a databases and if you are not used to working with databases every day that you may not be aware of just how many there are. To take a quick look at some of them you can just right-click a database in <b>SSMS &gt; select Tasks &gt;<\/b> and <b>Generate Scripts<\/b>.&nbsp;See the following figure.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0410.HSG-11-4-10-03.jpg\" border=\"0\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>From there you just have to select some objects to script out and then click the <b>Advanced<\/b> button when you find the scripting options the SMO has made available to you. As seen in the following figure.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8360.HSG-11-4-10-04.jpg\" border=\"0\" \/>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>To have all these options available to us we have to follow these steps.<\/p>\n<ol>\n<li>Load the<b>Microsoft.SqlServer.SMO<\/b> Assembly and then instantiate an instance of the<b>SMO ServerObject<\/b>. <\/li>\n<li>Give it the name of a database that we want to script out.<\/li>\n<li>Grab up all the tables, views, stored procedures, and functionsinto our<i>$Objects<\/i> variable.<\/li>\n<li>Start to build a directory to work in based on the current date and time so that we can script out the database again later and it will not overwrite anything.<\/li>\n<li>Grab the first object that we want to script out and start a loop.<\/li>\n<li>Instantiate an instance of the<b>ScripterObject<\/b>; and set a bunch of options for the objects that we want to script out.<\/li>\n<li>Finish building our directory structure and file name.<\/li>\n<li>Finally, we will script out one of the objects.<\/li>\n<\/ol>\n<p>See the following figure for an idea of how this will work.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1715.HSG-11-4-10-05.jpg\" border=\"0\" \/>&nbsp;<\/p>\n<p>For the most part this handles many the things that we may be interested in but I found some completely unexpected behavior when I flipped the&nbsp;<i>$scriptr<\/i><i>.<\/i><i>Options<\/i><i>.<\/i><i>ScriptDrops<\/i> option from&nbsp;<i>$False<\/i> to&nbsp;<i>$True<\/i>. Instead of getting a nice little drop statement at the top of the same script that I had generated previously, all that I got was the <b>Drop<\/b> statement. What I had to do was instantiate another scripter object every time that I passed through the&nbsp;<b>foreach<\/b> loop, generate the drop statement separately, and then just append it to the file.<\/p>\n<p>&nbsp;<\/p>\n<h2><a href=\"http:\/\/sqlvariant.com\/BlogSupport\/PoShDirtyDozen\/Script-DBObjectsIntoFolders.html\">Scripting Out the Whole Database from a Function<\/a><\/h2>\n<p>I&#8217;ve created the following script to make my life easier when I have to script out a database and especially when I then have to check those scripts into source control.&nbsp; It includes the drop statements that I needed:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">function global:Script-DBObjectsIntoFolders([string]$server, [string]$dbname){<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">[System.Reflection.Assembly]::LoadWithPartialName(&#8220;Microsoft.SqlServer.SMO&#8221;) | out-null<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;&nbsp;&nbsp; $SMOserver = New-Object (&#8216;Microsoft.SqlServer.Management.Smo.Server&#8217;) -argumentlist $server<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;&nbsp;&nbsp; $db = $SMOserver.databases[$dbname]<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$Objects = $db.Tables<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$Objects += $db.Views<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$Objects += $db.StoredProcedures<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$Objects += $db.UserDefinedFunctions<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">#Build this portion of the directory structure out here in case scripting takes more than one minute.<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$SavePath = &#8220;C:\\TEMP\\Databases\\&#8221; + $($dbname)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$DateFolder = get-date -format yyyyMMddHHmm<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">new-item -type directory -name &#8220;$DateFolder&#8221;-path &#8220;$SavePath&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">foreach ($ScriptThis in $Objects | where {!($_.IsSystemObject)}) {<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">#Need to Add Some mkDirs for the different $Fldr=$ScriptThis.GetType().Name <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr = new-object (&#8216;Microsoft.SqlServer.Management.Smo.Scripter&#8217;) ($SMOserver)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.AppendToFile = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.AllowSystemObjects = $False<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.ClusteredIndexes = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.DriAll = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.ScriptDrops = $False<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.IncludeHeaders = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.ToFileOnly = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.Indexes = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.Permissions = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.WithDependencies = $False<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&lt;#Script the Drop too#&gt;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ScriptDrop = new-object (&#8216;Microsoft.SqlServer.Management.Smo.Scripter&#8217;) ($SMOserver)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ScriptDrop.Options.AppendToFile = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ScriptDrop.Options.AllowSystemObjects = $False<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ScriptDrop.Options.ClusteredIndexes = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ScriptDrop.Options.DriAll = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ScriptDrop.Options.ScriptDrops = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ScriptDrop.Options.IncludeHeaders = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ScriptDrop.Options.ToFileOnly = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ScriptDrop.Options.Indexes = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ScriptDrop.Options.WithDependencies = $False<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&lt;#This section builds folder structures.&nbsp; Remove the date folder if you want to overwrite#&gt;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$TypeFolder=$ScriptThis.GetType().Name <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">if ((Test-Path -Path &#8220;$SavePath\\$DateFolder\\$TypeFolder&#8221;) -eq &#8220;true&#8221;) &#8216;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&#8220;Scripting Out $TypeFolder $ScriptThis&#8221;} &#8216;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;&nbsp;&nbsp; else {new-item -type directory -name &#8220;$TypeFolder&#8221;-path &#8220;$SavePath\\$DateFolder&#8221;}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ScriptFile = $ScriptThis -replace &#8220;\\[|\\]&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ScriptDrop.Options.FileName = &#8220;&#8221; + $($SavePath) + &#8220;\\&#8221; + $($DateFolder) + &#8220;\\&#8221; + $($TypeFolder) + &#8220;\\&#8221; + $($ScriptFile) + &#8220;.SQL&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.FileName = &#8220;$SavePath\\$DateFolder\\$TypeFolder\\$ScriptFile.SQL&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">#This is where each object actually gets scripted one at a time.<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ScriptDrop.Script($ScriptThis)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Script($ScriptThis)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">} #This ends the loop<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">} #This completes the function<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>In the script above I did a bit more than just add the drop to the loop. I also turned the whole script into a function. This enables me to call it <i>like<\/i> a stored procedure any time I want to script out a database on any of my Computers That Are Running SQL Server.&nbsp; Like this: <\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Script-DBObjectsIntoFolders &#8220;WIN7AS400&#8221; &#8220;AdventureWorksLT&#8221; <\/span><\/p>\n<p>&nbsp;<\/p>\n<p>I also included an example of variable expansion inside strings when I built the string for where it should store the file after it creates each object. If you examine <i>$ScriptDrop<\/i><i>.<\/i><i>Options<\/i><i>.<\/i><i>FileName<\/i> and <i>$scriptr<\/i><i>.<\/i><i>Options<\/i><i>.<\/i><i>FileName<\/i> you can see that they end up producing the same file name with two different approaches. The second approach is something that would be great to have in SQL. While I am building the string, Windows PowerShell can expand the variable and put whatever it is currently set to inside the string that I built.<\/p>\n<p>&nbsp;<\/p>\n<h2><a href=\"http:\/\/sqlvariant.com\/BlogSupport\/PoShDirtyDozen\/Script-AnObject.html\">Scripting Dependent Objects<\/a><\/h2>\n<p>One final feature that I want to make sure to point out is the <b>WithDependencies<\/b> option.&nbsp; When I use this feature I am typically only going after a single object (table, stored proc). When you set the <b>WithDependencies<\/b> option to <i>$True<\/i> the Scripter makes sure to include other database objects on which the object that you are trying to script out depends. This can be useful when you are trying to deploy a new table to production or when you have to change the data type of a column. After we have loaded up this function into our environment we can just call it as follows: <\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Script-AnObject &#8220;TransactionHistory&#8221; &#8220;AdventureWorks&#8221; &#8220;WIN7NetBook&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">function global:Script-AnObject([string]$dbname, [string]$server, [string]$objname){<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">[System.Reflection.Assembly]::LoadWithPartialName(&#8220;Microsoft.SqlServer.SMO&#8221;) | out-null<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$SMOserver = New-Object (&#8216;Microsoft.SqlServer.Management.Smo.Server&#8217;) -argumentlist $server<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$db = $SMOserver.databases[$dbname]<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$Objects = $db.Tables<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$Objects += $db.Views<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$Objects += $db.StoredProcedures<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$Objects += $db.UserDefinedFunctions<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">foreach ($ScriptThis in $Objects | where {!($_.IsSystemObject) -and $_.Name -eq &#8220;$objname&#8221;}) {<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr = new-object (&#8216;Microsoft.SqlServer.Management.Smo.Scripter&#8217;) ($SMOserver)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.AppendToFile = $False<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.AllowSystemObjects = $False<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.ClusteredIndexes = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.DriAll = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.ScriptDrops = $False<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.IncludeHeaders = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.ToFileOnly = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.Indexes = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.WithDependencies = $True<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ScriptFile = $ScriptThis -replace &#8220;\\[|\\]&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Options.FileName = &#8220;C:\\TEMP\\Databases\\&#8221; + $($dbname) + &#8220;\\&#8221; + $($ScriptFile) + &#8220;.SQL&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$scriptr.Script($ScriptThis)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2>Now, Start Scripting!<\/h2>\n<p>I hope that these capabilities and approaches are helpful in your environment and make your learning curve with Windows PowerShell somewhat easier. Please feel free to reach out to me on twitter <a href=\"https:\/\/twitter.com\/SQLvariant\">@SQL<i>variant<\/i><\/a> if you have questions around a use case that I did not cover or look at my <a href=\"http:\/\/sqlvariant.com\/wordpress\/\">blog<\/a> for more information.<\/p>\n<p>&nbsp;<\/p>\n<p>AA, that is all there is to using Windows PowerShell to script SQL Server databases.&nbsp; SQL guest blogger week will continue tomorrow when Sean McCown will talk about how to add users to DB.<\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a target=\"_blank\" href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> or post them on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: Learn how to use Windows PowerShell to automate creation of SQL Database objects such as tables, stored procedures, and functions. &nbsp; Hey, Scripting Guy! How can I use Windows PowerShell to script out SQL Server database objects? &#8212; AA &nbsp; Hello AA, Microsoft Scripting Guy Ed Wilson here. Today we have one final [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[194,146,56,3,176,45],"class_list":["post-16611","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-aaron-nelson","tag-databases","tag-guest-blogger","tag-scripting-guy","tag-sql-server","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Summary: Learn how to use Windows PowerShell to automate creation of SQL Database objects such as tables, stored procedures, and functions. &nbsp; Hey, Scripting Guy! How can I use Windows PowerShell to script out SQL Server database objects? &#8212; AA &nbsp; Hello AA, Microsoft Scripting Guy Ed Wilson here. Today we have one final [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=16611"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16611\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=16611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}