{"id":14031,"date":"2011-05-05T00:01:00","date_gmt":"2011-05-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/05\/05\/use-powershell-to-automatically-create-tempdb-files-in-sql-server\/"},"modified":"2011-05-05T00:01:00","modified_gmt":"2011-05-05T00:01:00","slug":"use-powershell-to-automatically-create-tempdb-files-in-sql-server","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-automatically-create-tempdb-files-in-sql-server\/","title":{"rendered":"Use PowerShell to Automatically Create Tempdb Files in SQL Server"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to automatically create Tempdb files in your SQL Server instance.<\/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\" \/>&nbsp; Hey, Scripting Guy! How can I easily create multiple tempdb data files in my SQL Server instance?<\/p>\n<p>&mdash;WM<\/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 WM, Microsoft Scripting Guy, Ed Wilson, is here. What a great week of SQL guest bloggers, and we have Michael Wells here today to answer your question. <\/p>\n<blockquote>\n<p>Michael is a database administrator for a large financial firm in the Tampa Bay area. He has been working in IT since 2000, and he has administered all versions of SQL Server, from 2000 and up. Michael runs the Sarasota SQL Server User Group located in Sarasota, FL and has presented at numerous SQL Saturday, CodeCamp, and User Group events. <br \/>Contact information: <a target=\"_blank\" href=\"http:\/\/sqltechmike.com\">http:\/\/sqltechmike.com<\/a><\/p>\n<\/blockquote>\n<p>Take it away, Michael&hellip;<\/p>\n<p>There is a lot of conflicting advice regarding the proper configuration of tempdb. Most experts agree that creating multiple tempdb data files is one part of relieving I\/O contention in tempdb. Because you have already done your due diligence and decided that this is the right action to take, let us take a look at how you can go about creating multiple tempdbs via Windows PowerShell scripting.<\/p>\n<p>The full script can be downloaded from the Scripting Guy&rsquo;s <a target=\"_blank\" href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/0c83a4ed-709d-416e-afcf-d160c18ef0fc\">Script Repository<\/a>, but for demonstration purposes we will break it into the following steps:<\/p>\n<ol>\n<li>Identifying the parameters<\/li>\n<li>Determining the CPU and memory specifications<\/li>\n<li>Calculating the number of files needed<\/li>\n<li>Building the SQL command<\/li>\n<\/ol>\n<h3>Identifying the Parameters <\/h3>\n<p>Before we begin building the script, let us talk about the optional parameters that can be used to customize the calculation.<\/p>\n<ol>\n<li>maxFileCount \n<ol>\n<li>Used to set an upper limit on the number of files created<\/li>\n<li>Default value: 16<\/li>\n<\/ol>\n<\/li>\n<li>maxFileInitialSizeMB \n<ol>\n<li>Used to set an upper limit on the initial file size<\/li>\n<li>Default value: 1024<\/li>\n<\/ol>\n<\/li>\n<li>maxFileGrowthSizeMB \n<ol>\n<li>Used to set the file growth limit<\/li>\n<li>Default value: 2048<\/li>\n<\/ol>\n<\/li>\n<li>fileGrowthMB \n<ol>\n<li>Used to set the file growth rate<\/li>\n<li>Default value: 512<\/li>\n<\/ol>\n<\/li>\n<li>coreMultiplier \n<ol>\n<li>Used to specify how many files to create per processor core<\/li>\n<li>Default value: 1.0<\/li>\n<\/ol>\n<\/li>\n<li>outClipboard \n<ol>\n<li>Loads the output into the clipboard instead of returning it to the screen<\/li>\n<li>Default value: Off<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h3><b>Determining the CPU and Memory Specifications<\/b><\/h3>\n<p>First, we will look at the number of processor cores and system memory. One of the easiest ways to do this is to use the <b>Get-WMIObject<\/b> cmdlet. The <b>Get-WMIObject<\/b> cmdlet allows you to interact with the Windows Management Instrumentation (WMI) the same way you would work with any other object&mdash;via properties and methods. For more information, you can go to the <a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394572(v=VS.85).aspx\">WMI Reference<\/a> in the MSDN Library, or you can dive right in by starting with the following command to list the WMI classes that are available on your system. <\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 11pt;list-style-type: disc;margin: 0in 0in 10pt\"><span style=\"font-family: Courier New\"><b><span style=\"line-height: 12pt\"><span style=\"color: #5f9ea0\"><span>Get-WmiObject<\/span><\/span><\/span><\/b><span><span style=\"line-height: 12pt\"><span style=\"color: #000000\"> <\/span><\/span><i><span style=\"line-height: 12pt\"><span style=\"color: #5f9ea0\">-List<\/span><\/span><\/i><span style=\"line-height: 12pt\"><span style=\"color: #000000\"> | <\/span><\/span><\/span><span style=\"line-height: 12pt\"><span style=\"color: #5f9ea0\">more<\/span><\/span><\/span><span style=\"line-height: 12pt\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Calibri\"><span style=\"color: #000000\">Let us use the cmdlet to access the Win32_Processor and Win32_OperatingSystem classes to get the information that we need.<\/span><\/span><\/span><span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #008000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #008000\">#get a collection of physical processors<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span style=\"color: #000000\"><span>[<\/span><\/span><\/span><span><span><span style=\"color: #008080\">array<\/span><\/span><span><span style=\"color: #000000\">] <\/span><\/span><span><span style=\"color: #800080\">$procs<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">=<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><b><span><span style=\"color: #5f9ea0\">Get-WmiObject<\/span><\/span><\/b><span><span style=\"color: #000000\"> <\/span><\/span><\/span><span><span style=\"color: #800000\">Win32_Processor<\/span><\/span><\/span><span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span style=\"color: #800080\"><span>$totalProcs<\/span><\/span><\/span><span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">=<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #800080\">$procs<\/span><\/span><span><span style=\"color: #000000\">.<\/span><\/span><\/span><span><span style=\"color: #8b4513\">Count<\/span><\/span><\/span><span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span style=\"color: #800080\"><span>$totalCores<\/span><\/span><\/span><span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">=<\/span><\/span><\/span><span><span style=\"color: #000000\"> 0<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #008000\">#count the total number of cores across all processors<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span style=\"color: #0000ff\"><span>foreach<\/span><\/span><\/span><span><span><span style=\"color: #000000\"> (<\/span><\/span><span><span style=\"color: #800080\">$proc<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #0000ff\">in<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #800080\">$procs<\/span><\/span><\/span><span><span style=\"color: #000000\">)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #000000\">{<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><span><span><span style=\"color: #800080\">$totalCores<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">=<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #800080\">$totalCores<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">+<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #800080\">$proc<\/span><\/span><span><span style=\"color: #000000\">.<\/span><\/span><\/span><span><span style=\"color: #8b4513\">NumberOfCores<\/span><\/span><\/span><span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #000000\">}<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #008000\">#get the amount of total memory (MB) <\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span style=\"color: #800080\"><span>$wmi<\/span><\/span><\/span><span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">=<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><b><span><span style=\"color: #5f9ea0\">Get-WmiObject<\/span><\/span><\/b><span><span style=\"color: #000000\"> <\/span><\/span><\/span><span><span style=\"color: #800000\">Win32_OperatingSystem<\/span><\/span><\/span><span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 11pt;list-style-type: disc;margin: 0in 0in 10pt\"><span style=\"font-family: Courier New\"><span style=\"line-height: 12pt\"><span style=\"color: #800080\"><span>$totalMemory<\/span><\/span><\/span><span><span style=\"line-height: 12pt\"><span style=\"color: #000000\"> <\/span><\/span><span style=\"line-height: 12pt\"><span style=\"color: #ff0000\">=<\/span><\/span><span style=\"line-height: 12pt\"><span style=\"color: #000000\"> (<\/span><\/span><span style=\"line-height: 12pt\"><span style=\"color: #800080\">$wmi<\/span><\/span><span style=\"line-height: 12pt\"><span style=\"color: #000000\">.<\/span><\/span><span style=\"line-height: 12pt\"><span style=\"color: #8b4513\">TotalVisibleMemorySize<\/span><\/span><span style=\"line-height: 12pt\"><span style=\"color: #000000\"> <\/span><\/span><span style=\"line-height: 12pt\"><span style=\"color: #ff0000\">\/<\/span><\/span><\/span><span style=\"line-height: 12pt\"><span style=\"color: #000000\"> 1024)<\/span><\/span><\/span><span style=\"line-height: 12pt\"><\/span><\/p>\n<\/blockquote>\n<p>This section of code creates an array of Win32_Processor WMI Objects. This array will contain one instance for each physical processor (socket) that is present on the system. Next, we will loop through the array, counting the number of cores on each processor. This will give us a count of the total number of cores in the system. This count ignores hyper-threading, which would otherwise provide an undesired result. We then use the Win32_OperatingSystem object to give us the TotalVisibleMemorySize value, which corresponds to the amount of memory that is seen by the operating system. This will prevent our calculation from being thrown off when a system has more physical memory than the operating system can use (such as a 32-bit system with 4 GB of RAM). <\/p>\n<h3><b>Calculating the Number of Files Needed<\/b><\/h3>\n<p>Now we need to take the values that we have obtained and apply them along with the parameter options passed in originally so that we can determine how many data files we should create and what size they should be. To accomplish this we will use the following formulas:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 11pt;list-style-type: disc;margin: 0in 0in 10pt\"><span style=\"line-height: 12pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">File Count = Number of Cores * Core Multiplier<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 11pt;list-style-type: disc;margin: 0in 0in 10pt\"><span style=\"line-height: 12pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">File Size = Total Memory \/ File Count<\/span><\/span><\/span><span style=\"line-height: 12pt\"><\/span><\/p>\n<\/blockquote>\n<p>This basic logic will work for most cases, but we should add some additional logic to handle those situations where the resulting values are too large or too small. My chosen implementation can be viewed in the following example.<\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;text-indent: 0.5in;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #008000\">#calculate the number of files needed (= number of procs)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><span><span><span style=\"color: #800080\">$fileCount<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">=<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #800080\">$totalCores<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">*<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><\/span><span><span style=\"color: #800080\">$coreMultiplier<\/span><\/span><\/span><span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><span><span><span style=\"color: #0000ff\">if<\/span><\/span><span><span style=\"color: #000000\"> (<\/span><\/span><span><span style=\"color: #800080\">$fileCount<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">-gt<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #800080\">$maxFileCount<\/span><\/span><\/span><span><span style=\"color: #000000\">)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>{<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><span><span><span style=\"color: #800080\">$fileCount<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">=<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><\/span><span><span style=\"color: #800080\">$maxFileCount<\/span><\/span><\/span><span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><span><span style=\"color: #008000\">#calculate file size (total memory \/ number of files)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><span><span><span style=\"color: #800080\">$fileSize<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">=<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #800080\">$totalMemory<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">\/<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><\/span><span><span style=\"color: #800080\">$fileCount<\/span><\/span><\/span><span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><span><span><span style=\"color: #0000ff\">if<\/span><\/span><span><span style=\"color: #000000\"> (<\/span><\/span><span><span style=\"color: #800080\">$fileSize<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">-gt<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #800080\">$maxFileInitialSizeMB<\/span><\/span><\/span><span><span style=\"color: #000000\">)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>{<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><span><span><span style=\"color: #800080\">$fileSize<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">=<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><\/span><span><span style=\"color: #800080\">$maxFileInitialSizeMB<\/span><\/span><\/span><span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 11pt;list-style-type: disc;margin: 0in 0in 10pt\"><span style=\"line-height: 12pt\"><span style=\"font-family: Courier New\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>}<\/span><\/span><\/span><\/span><\/p>\n<h3><b>Building the SQL Command<\/b><\/h3>\n<p>Finally, we take values that were calculated previously and use them to build the actual T-SQL script.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #008000\">#build the sql command<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><span><span><span style=\"color: #800080\">$command<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">=<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><\/span><span><span style=\"color: #800000\">&#8220;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #800000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>declare @data_path varchar(300);<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #800000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #800000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>select <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #800000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>@data_path = replace([filename], &#8216;.mdf&#8217;,&#8221;)<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #800000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>from <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #800000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>sysaltfiles s<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #800000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>where<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #800000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>name = &#8216;tempdev&#8217;;<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #800000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #800000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>ALTER DATABASE [tempdb] MODIFY FILE ( NAME = N&#8217;tempdev&#8217;, SIZE = {0}MB , MAXSIZE = {1}MB, FILEGROWTH = {2}MB );<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span style=\"color: #800000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>&#8220;<\/span><\/span><\/span><span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">-f<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #800080\">$fileSize<\/span><\/span><span><span style=\"color: #000000\">, <\/span><\/span><span><span style=\"color: #800080\">$maxFileGrowthSizeMB<\/span><\/span><span><span style=\"color: #000000\">, <\/span><\/span><\/span><span><span style=\"color: #800080\">$fileGrowthMB<\/span><\/span><\/span><span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><span><span><span style=\"color: #0000ff\">for<\/span><\/span><span><span style=\"color: #000000\"> (<\/span><\/span><span><span style=\"color: #800080\">$i<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">=<\/span><\/span><span><span style=\"color: #000000\"> 2; <\/span><\/span><span><span style=\"color: #800080\">$i<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">-le<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #800080\">$fileCount<\/span><\/span><span><span style=\"color: #000000\">; <\/span><\/span><span><span style=\"color: #800080\">$i<\/span><\/span><\/span><span><span style=\"color: #000000\">++)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>{<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><span><span><span style=\"color: #800080\">$command<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">=<\/span><\/span><span><span><span style=\"color: #000000\">&nbsp; <\/span><\/span><\/span><span><span style=\"color: #800080\">$command<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">+<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><\/span><span><span style=\"color: #800000\">&#8220;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #800000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>declare @stmnt{3} nvarchar(500)<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #800000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>select @stmnt{3} = N&#8217;ALTER DATABASE [tempdb] ADD FILE ( NAME = N&#8221;tempdev{3}&#8221;, FILENAME = &#8221;&#8217; + @data_path + &#8216;{3}.mdf&#8221; , SIZE = {0}MB , MAXSIZE = {1}MB, FILEGROWTH = {2}MB )&#8217;;<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #800000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>exec sp_executesql @stmnt{3};<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Courier New\"><span><span style=\"color: #800000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span><span>&nbsp;&nbsp;&nbsp; <\/span>&#8220;<\/span><\/span><\/span><span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">-f<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #800080\">$fileSize<\/span><\/span><span><span style=\"color: #000000\">, <\/span><\/span><span><span style=\"color: #800080\">$maxFileGrowthSizeMB<\/span><\/span><span><span style=\"color: #000000\">, <\/span><\/span><span><span style=\"color: #800080\">$fileGrowthMB<\/span><\/span><span><span style=\"color: #000000\">, <\/span><\/span><span><span style=\"color: #800080\">$i<\/span><\/span><\/span><span><span><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Courier New\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 11pt;list-style-type: disc;margin: 0in 0in 10pt\"><span style=\"font-family: Courier New\"><span style=\"line-height: 12pt\"><span><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><span><span style=\"line-height: 12pt\"><span style=\"color: #0000ff\">return<\/span><\/span><span style=\"line-height: 12pt\"><span style=\"color: #000000\"> <\/span><\/span><\/span><span style=\"line-height: 12pt\"><span style=\"color: #800080\">$command<\/span><\/span><\/span><span style=\"line-height: 12pt\"><\/span><\/p>\n<\/blockquote>\n<p>The script output is really a batch that consists of a statement that gets the path to the current tempdb data file, a statement that resizes the current tempdb data file, and when necessary, a group of statements to create the additional files. This output can now be pasted into SQL Management Studio or loaded into a variable and passed to another Windows PowerShell function such as <b>Invoke-SqlCmd<\/b> or <a target=\"_blank\" href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/7985b7ef-ed89-4dfd-b02a-433cc4e30894\">Invoke-SqlCmd2<\/a><a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/7985b7ef-ed89-4dfd-b02a-433cc4e30894\">.<\/a><\/p>\n<h3><b>Using the Function<\/b><\/h3>\n<p>There are a couple of different ways to use this function, based on your personal preference. The simplest path is to call the function by using the <b>outClipboard<\/b> switch, and then pasting it inside your query editor of choice. This is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7536.Wells1_4962D9CE.jpg\"><img decoding=\"async\" height=\"392\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4087.Wells1_thumb_3B2460DE.jpg\" alt=\"Image of command\" border=\"0\" title=\"Image of command\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" \/><\/a><\/p>\n<p>The other option is to create a local variable to hold the result, and then pass it to another function like <b>Invoke-SqlCmd2<\/b> as shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7624.wells2_4D00F4AB.jpg\"><img decoding=\"async\" height=\"392\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6646.wells2_thumb_21BC5DA4.jpg\" alt=\"Image of command\" border=\"0\" title=\"Image of command\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" \/><\/a><\/p>\n<p>Whichever method you chose, when you run the statement against your SQL Server, you should see the following results in tempdb.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8787.wells3_6CAFCE73.jpg\"><img decoding=\"async\" height=\"332\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3581.wells3_thumb_0C5EA83C.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" \/><\/a><\/p>\n<p>This post showed one possible solution for creating additional tempdb data files on your SQL Server instance by using Windows PowerShell. The <b>Set-TempDbSize<\/b> function can be adapted and customized based on your organization&rsquo;s standards. <\/p>\n<p>If you are unsure as to whether you should increase the number of tempdb files in your situation, I recommend that you look into the sys.dm_io_virtual_file_stats dmv. You can also learn more about how to decide the appropriate number of tempdb data files and the many factors that you should take into consideration by reading this detailed blog <a target=\"_blank\" href=\"http:\/\/www.sqlskills.com\/BLOGS\/PAUL\/post\/A-SQL-Server-DBA-myth-a-day-(1230)-tempdb-should-always-have-one-data-file-per-processor-core.aspx\">post<\/a> from Paul Randal.<\/p>\n<p>Thank you, Michael, for writing this excellent and useful guest blog. <\/p>\n<p>WM, that is all there is to using Windows PowerShell to create easily multiple tempdb files on SQL Server. SQL Guest Blogger week will continue tomorrow when Yan Pan will talk to us. <\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to automatically create Tempdb files in your SQL Server instance. &nbsp; Hey, Scripting Guy! How can I easily create multiple tempdb data files in my SQL Server instance? &mdash;WM Hello WM, Microsoft Scripting Guy, Ed Wilson, is here. What a great week of SQL guest bloggers, and we [&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":[146,56,253,3,176,45],"class_list":["post-14031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-databases","tag-guest-blogger","tag-michael-wells","tag-scripting-guy","tag-sql-server","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to automatically create Tempdb files in your SQL Server instance. &nbsp; Hey, Scripting Guy! How can I easily create multiple tempdb data files in my SQL Server instance? &mdash;WM Hello WM, Microsoft Scripting Guy, Ed Wilson, is here. What a great week of SQL guest bloggers, and we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/14031","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=14031"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/14031\/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=14031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=14031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=14031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}