<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'PowerShell' and 'Scripts'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=PowerShell,Scripts&amp;orTags=0</link><description>Search results matching tags 'PowerShell' and 'Scripts'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>PowerShell for the DBA: Search the Windows Event Logs for Errors</title><link>http://sqlblog.com/blogs/buck_woody/archive/2010/08/04/powershell-for-the-dba-search-the-windows-event-logs-for-errors.aspx</link><pubDate>Wed, 04 Aug 2010 13:43:30 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:27605</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p&gt;This is a very simple script - but it's one I run each morning. It searches the Windows System Event Log for an error condition. You can replace "System" here with "Application" or "Security", or any of the other logs that are created on your Windows Server. This is run at the server, since I have each server check itself and make a file of the results - then I swing by and pick up the files each morning with another script that builds a web page.&lt;/p&gt;
&lt;p&gt;&lt;span style="color:#339966;"&gt;&amp;nbsp;# Parse for errors&lt;br /&gt;Get-eventlog System | Where-Object { $_.entryType -eq "error" }&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;"&gt;&lt;span style="font-family:Calibri;"&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-size:11.0pt;mso-bidi-font-family:Calibri;mso-ansi-language:EN;"&gt;Script Disclaimer, for people who need to be told this sort of thing: &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:Calibri;mso-ansi-language:EN;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:Calibri;mso-ansi-language:EN;"&gt;&lt;span style="font-family:Calibri;"&gt;Never trust any script, including those that you find here, until you understand exactly what it does and how it will act on your systems. Always check the script on a test system or Virtual Machine, not a production system. Yes, there are always multiple ways to do things, and this script may not work in every situation, for everything. It&amp;rsquo;s just a script, people. All scripts on this site are performed by a professional stunt driver on a closed course. Your mileage may vary. Void where prohibited. Offer good for a limited time only. Keep out of reach of small children. Do not operate heavy machinery while using this script. If you experience blurry vision, indigestion or diarrhea&amp;nbsp;during the operation of this script, see a physician immediately.&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;</description></item><item><title>PowerShell and Extended Properties</title><link>http://sqlblog.com/blogs/buck_woody/archive/2010/07/20/powershell-and-extended-properties.aspx</link><pubDate>Tue, 20 Jul 2010 12:43:33 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:27176</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="font-family:Calibri;font-size:small;"&gt;I use Extended Properties on databases and their objects all the time. They are a great way to include information about the object &amp;ndash; I use them for versioning the database, detailing what a column is used for and so on. They can be a little tricky to set, but it&amp;rsquo;s really not bad once you learn how.&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;Ken Simmons, a SQL Server MVP has a fantastic article here that explains more: &lt;/span&gt;&lt;a href="http://cybersql.blogspot.com/2010/07/extended-property-awesomeness.html"&gt;&lt;span style="font-family:Times New Roman;color:#0000ff;font-size:small;"&gt;http://cybersql.blogspot.com/2010/07/extended-property-awesomeness.html&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Calibri;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family:Calibri;font-size:small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Calibri;"&gt;Although it&amp;rsquo;s not as simple as I would like to set the properties using PowerShell, it&amp;rsquo;s really trivial to read them. I do this to make system documentation in an HTML page. Open a SQL Server 2008 Powershell provider (sqlps.exe) and CD to any &amp;ldquo;directory&amp;rdquo; of a database or tables, views, whatever. Then you can just type this: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="text-indent:0.5in;margin:0in 0in 0pt;"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;span style="color:#00b050;"&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Calibri;"&gt;DIR | select-object &amp;ndash;property name, Extendedproperties&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="text-indent:0.5in;margin:0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family:Calibri;font-size:small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Calibri;"&gt;As always, do this on a test system.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family:Calibri;font-size:small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;"&gt;&lt;span style="font-family:Calibri;"&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:Calibri;mso-bidi-font-size:11.0pt;mso-ansi-language:EN;"&gt;Script Disclaimer, for people who need to be told this sort of thing: &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:Calibri;mso-ansi-language:EN;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:Calibri;mso-ansi-language:EN;"&gt;&lt;span style="font-family:Calibri;"&gt;Never trust any script, including those that you find here, until you understand exactly what it does and how it will act on your systems. Always check the script on a test system or Virtual Machine, not a production system. Yes, there are always multiple ways to do things, and this script may not work in every situation, for everything. It&amp;rsquo;s just a script, people. All scripts on this site are performed by a professional stunt driver on a closed course. Your mileage may vary. Void where prohibited. Offer good for a limited time only. Keep out of reach of small children. Do not operate heavy machinery while using this script. If you experience blurry vision, indigestion or diarrhea&amp;nbsp;during the operation of this script, see a physician immediately.&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;span style="font-family:'Times New Roman','serif';font-size:12pt;mso-ansi-language:EN;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Create and Track Your Own License Keys with PowerShell</title><link>http://sqlblog.com/blogs/buck_woody/archive/2010/04/15/create-and-track-your-own-license-keys-with-powershell.aspx</link><pubDate>Thu, 15 Apr 2010 13:10:45 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:24316</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p&gt;SQL Server used to have&amp;#160; cool little tool that would let you track your licenses. Microsoft didn’t use it to limit your system or anything, it was just a place on the server where you could put that this system used this license key. I miss those days – we don’t track that any more, and I want to make sure I’m up to date on my licensing, so I made my own.&lt;/p&gt;  &lt;p&gt;Now, there are a LOT of ways you could do this. You could add an extended property in SQL Server, add a table to a tracking database, use a text file, track it somewhere else, whatever. This is just the route I chose; if you want to use some other method, feel free. Just sharing here.&lt;/p&gt;  &lt;p&gt;&lt;font color="#ff0000"&gt;&lt;strong&gt;Warning&lt;/strong&gt; Serious problems might occur if you modify the registry incorrectly by using Registry Editor or by using another method. These problems &lt;strong&gt;might require that you reinstall the operating system&lt;/strong&gt;. Microsoft cannot guarantee that these problems can be solved. Modify the registry &lt;em&gt;&lt;u&gt;at your own risk&lt;/u&gt;&lt;/em&gt;.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;And this is REALLY important. I include a disclaimer at the end of my scripts, but in this case you’re modifying your registry, and that could be EXTREMELY dangerous – only do this on a test server – and I’m just showing you how I did mine. It isn’t an endorsement or anything like that, and this is a “Buck Woody” thing, NOT a Microsoft thing. &lt;a href="http://support.microsoft.com/kb/256986" target="_blank"&gt;See this link first&lt;/a&gt;, and then you can read on.&lt;/p&gt;  &lt;p&gt;OK, here’s my script:&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:navy;font-size:11pt;"&gt;# Track your own licenses&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:navy;font-size:11pt;"&gt;# Write a New Key to be the License Location&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;mkdir HKCU:\SOFTWARE\Buck&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;&amp;#160;&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:navy;font-size:11pt;"&gt;# Write the variables - one sets the type, the other sets the number, and the last one holds the key &lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;New-ItemProperty HKCU:\SOFTWARE\Buck -name &amp;quot;SQLServerLicenseType&amp;quot; -value &amp;quot;Processor&amp;quot;&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:navy;font-size:11pt;"&gt;# Notice the Dword value here - this one is a number so it needs that. Keep this on one line!&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;New-ItemProperty HKCU:\SOFTWARE\Buck -name &amp;quot;SQLServerLicenseNumber&amp;quot; -propertytype DWord -value 4&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;New-ItemProperty HKCU:\SOFTWARE\Buck -name &amp;quot;SQLServerLicenseKey&amp;quot; -value &amp;quot;ABCD1234&amp;quot;&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;&amp;#160;&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:navy;font-size:11pt;"&gt;# Read them all&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;$LicenseKey = Get-Item HKCU:\Software\Buck&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;$Licenses = Get-ItemProperty $LicenseKey.PSPath&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;foreach ($License in $LicenseKey.Property) { $License + &amp;quot;=&amp;quot; + $Licenses.$License }&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;&amp;#160;&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;" class="MsoNormal"&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-size:11.0pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;" class="MsoNormal"&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-size:11.0pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;Script Disclaimer, for people who need to be told this sort of thing: &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;         &lt;p&gt;&lt;/p&gt;       &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;Never trust any script, including those that you find here, until you understand exactly what it does and how it will act on your systems. Always check the script on a test system or Virtual Machine, not a production system. Yes, there are always multiple ways to do things, and this script may not work in every situation, for everything. It’s just a script, people. All scripts on this site are performed by a professional stunt driver on a closed course. Your mileage may vary. Void where prohibited. Offer good for a limited time only. Keep out of reach of small children. Do not operate heavy machinery while using this script. If you experience blurry vision, indigestion or diarrhea during the operation of this script, see a physician immediately.&lt;/span&gt;&lt;/i&gt;&lt;span style="font-family:'Times New Roman','serif';font-size:12pt;mso-ansi-language:en;"&gt;     &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;</description></item><item><title>List SQL Server Instances using the Registry</title><link>http://sqlblog.com/blogs/buck_woody/archive/2010/03/31/list-sql-server-instances-using-the-registry.aspx</link><pubDate>Wed, 31 Mar 2010 13:41:24 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:23906</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p&gt;I read &lt;a href="http://thepowershellguy.com/blogs/posh/archive/2007/06/20/remote-registry-access-and-creating-new-registry-values-with-powershell.aspx" target="_blank"&gt;this interesting article on using PowerShell and the registry&lt;/a&gt;, and thought I would modify his information a bit to list the SQL Server Instances on a box. The interesting thing about listing instances this was is that you can touch remote machines, find the instances when they are off and so on. Anyway, here’s the scriptlet I used to find the Instances on my system:&lt;/p&gt;  &lt;p&gt;$MachineName = '.'&lt;/p&gt;  &lt;p&gt;$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $MachineName)&lt;/p&gt;  &lt;p&gt;$regKey= $reg.OpenSubKey(&amp;quot;SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL&amp;quot; )&lt;/p&gt;  &lt;p&gt;$regkey.GetValueNames()&lt;/p&gt;  &lt;p&gt;You can read more of his article to find out the reason for the remote registry call and so forth – there are also security implications here for being able to read the registry.&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;" class="MsoNormal"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-size:11.0pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;Script Disclaimer, for people who need to be told this sort of thing: &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;           &lt;p&gt;&lt;/p&gt;         &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;&lt;font face="Calibri"&gt;Never trust any script, including those that you find here, until you understand exactly what it does and how it will act on your systems. Always check the script on a test system or Virtual Machine, not a production system. Yes, there are always multiple ways to do things, and this script may not work in every situation, for everything. It’s just a script, people. All scripts on this site are performed by a professional stunt driver on a closed course. Your mileage may vary. Void where prohibited. Offer good for a limited time only. Keep out of reach of small children. Do not operate heavy machinery while using this script. If you experience blurry vision, indigestion or diarrhea during the operation of this script, see a physician immediately.&lt;/font&gt;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;</description></item><item><title>Open the SQL Server Error Log with PowerShell</title><link>http://sqlblog.com/blogs/buck_woody/archive/2010/03/29/open-the-sql-server-error-log-with-powershell.aspx</link><pubDate>Mon, 29 Mar 2010 13:35:52 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:23836</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p&gt;Using the Server Management Objects (SMO) library, you don’t even need to have the SQL Server 2008 PowerShell Provider to read the SQL Server Error Logs – in fact, you can use regular old everyday PowerShell. Keep in mind you &lt;strong&gt;will&lt;/strong&gt; need the SMO libraries – which can be installed separately or by installing the Client Tools from the SQL Server install media. You could search for errors, store a result as a variable, or act on the returned values in some other way.&lt;/p&gt;  &lt;p&gt;Replace the Machine Name with your server and Instance Name with your instance, but leave the quotes, to make this work on your system:&lt;/p&gt;  &lt;p&gt;&lt;font color="#008000"&gt;[reflection.assembly]::LoadWithPartialName(&amp;quot;Microsoft.SqlServer.Smo&amp;quot;)     &lt;br /&gt;$machineName = &amp;quot;UNIVAC&amp;quot;      &lt;br /&gt;$instanceName = &amp;quot;Production&amp;quot;      &lt;br /&gt;$sqlServer = new-object (&amp;quot;Microsoft.SqlServer.Management.Smo.Server&amp;quot;) &amp;quot;$machineName\$instanceName&amp;quot;      &lt;br /&gt;$sqlServer.ReadErrorLog()&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Want to search for something specific, like the word “Error”? Replace the last line with this:&lt;/p&gt;  &lt;p&gt;&lt;font color="#008000"&gt;$sqlServer.ReadErrorLog() | where {$_.Text -like &amp;quot;Error*&amp;quot;}&lt;/font&gt;&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;" class="MsoNormal"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-size:11.0pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;Script Disclaimer, for people who need to be told this sort of thing: &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;           &lt;p&gt;&lt;/p&gt;         &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;&lt;font face="Calibri"&gt;Never trust any script, including those that you find here, until you understand exactly what it does and how it will act on your systems. Always check the script on a test system or Virtual Machine, not a production system. Yes, there are always multiple ways to do things, and this script may not work in every situation, for everything. It’s just a script, people. All scripts on this site are performed by a professional stunt driver on a closed course. Your mileage may vary. Void where prohibited. Offer good for a limited time only. Keep out of reach of small children. Do not operate heavy machinery while using this script. If you experience blurry vision, indigestion or diarrhea during the operation of this script, see a physician immediately.&lt;/font&gt;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;</description></item><item><title>PowerShell: Read Excel to Create Inserts</title><link>http://sqlblog.com/blogs/buck_woody/archive/2010/03/18/powershell-read-excel-to-create-inserts.aspx</link><pubDate>Thu, 18 Mar 2010 12:09:36 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:23520</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p&gt;I’m writing &lt;a href="http://www.informit.com/guides/content.aspx?g=sqlserver&amp;amp;seqNum=342" target="_blank"&gt;a series of articles on how to migrate “departmental” data into SQL Server&lt;/a&gt;. I also hold workshops on the entire process – from discovering that the data exists to the modeling process and then how to design the Extract, Transform and Load (ETL) process. Finally I write about (and teach) a few methods on actually moving the data.&lt;/p&gt;  &lt;p&gt;One of those options is to use PowerShell. There are a lot of ways even with that choice, but the one I show is to read two columns from the spreadsheet and output statements that would insert the data using a stored procedure. Of course, you could re-write this as INSERT statements, out to a text file for bcp, or even use a database connection in the script to move the data directly from Excel into SQL Server. &lt;/p&gt;  &lt;p&gt;This snippet won’t run on your system, of course – it assumes a Microsoft Office Excel 2007 spreadsheet located at &lt;strong&gt;c:\temp&lt;/strong&gt; called &lt;strong&gt;VendorList.xlsx&lt;/strong&gt;. It looks for a tab in that spreadsheet called &lt;strong&gt;Vendors&lt;/strong&gt;. The statement that does the writing just uses one column: &lt;strong&gt;Vendor Code&lt;/strong&gt;. Here’s the breakdown of what I’m doing:&lt;/p&gt;  &lt;p&gt;In the first block, I connect to Microsoft Office Excel. That connection string is specific to Excel 2007, so if you need a different version you’ll need to look that up.&lt;/p&gt;  &lt;p&gt;In the second block I set up a selection from the entire spreadsheet based on that tab. Note that if you’re only after certain data you shouldn’t get the whole spreadsheet – that’s just good practice.&lt;/p&gt;  &lt;p&gt;In the next block I create the text I want, inserting the Vendor Code field as I go.&lt;/p&gt;  &lt;p&gt;Finally I close the connection.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;  &lt;p&gt;&lt;font color="#408080"&gt;$ExcelConnection= New-Object -com &amp;quot;ADODB.Connection&amp;quot;      &lt;br /&gt;$ExcelFile=&amp;quot;c:\temp\VendorList.xlsx&amp;quot;       &lt;br /&gt;$ExcelConnection.Open(&amp;quot;Provider=Microsoft.ACE.OLEDB.12.0;`       &lt;br /&gt;Data Source=$ExcelFile;Extended Properties=Excel 12.0;&amp;quot;) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#408080"&gt;$strQuery=&amp;quot;Select * from [Vendors$]&amp;quot;      &lt;br /&gt;$ExcelRecordSet=$ExcelConnection.Execute($strQuery) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#408080"&gt;do {      &lt;br /&gt;Write-Host &amp;quot;EXEC sp_InsertVendors '&amp;quot; $ExcelRecordSet.Fields.Item(&amp;quot;Vendor Code&amp;quot;).Value &amp;quot;'&amp;quot;      &lt;br /&gt;$ExcelRecordSet.MoveNext()}       &lt;br /&gt;Until ($ExcelRecordSet.EOF) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#408080"&gt;$ExcelConnection.Close()&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-size:11.0pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;Script Disclaimer, for people who need to be told this sort of thing: &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;           &lt;p&gt;&lt;/p&gt;         &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;&lt;font face="Calibri"&gt;Never trust any script, including those that you find here, until you understand exactly what it does and how it will act on your systems. Always check the script on a test system or Virtual Machine, not a production system. All scripts on this site are performed by a professional stunt driver on a closed course. Your mileage may vary. Void where prohibited. Offer good for a limited time only. Keep out of reach of small children. Do not operate heavy machinery while using this script. If you experience blurry vision, indigestion or diarrhea during the operation of this script, see a physician immediately.&lt;/font&gt;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;</description></item><item><title>PowerShell Version Two – Get Continuous Perf Counters</title><link>http://sqlblog.com/blogs/buck_woody/archive/2010/02/25/powershell-version-two-get-continuous-perf-counters.aspx</link><pubDate>Thu, 25 Feb 2010 14:32:52 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:22620</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p&gt;In version 2.0 of PowerShell, you can now use a direct command-let (get-Counter) to get at the Performance Monitor counters. For instance, to show the current value of the Processor Percent Time, use this command:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;span style="color:blue;"&gt;Get-Counter&lt;/span&gt; &lt;span style="color:darkred;"&gt;'\Processor(*)\% Processor Time'&lt;/span&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;The interesting part of get-Counter is that you can add a parameter at the end to keep sampling:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;span style="color:blue;"&gt;Get-Counter&lt;/span&gt; &lt;span style="color:darkred;"&gt;'\Processor(*)\% Processor Time' - Continuous&lt;/span&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;Which is very useful if you’re doing performance troubleshooting like I am this morning. And yes, you can use this to get at SQL Server counters as well – to see the entire set of counters, use this command:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;span style="color:blue;"&gt;Get-Counter&lt;/span&gt; &lt;span style="color:navy;"&gt;–listSet&lt;/span&gt; &lt;span style="color:blueviolet;"&gt;*&lt;/span&gt; &lt;span style="color:darkgray;"&gt;|&lt;/span&gt; &lt;span style="color:blue;"&gt;Select-Object&lt;/span&gt; &lt;span style="color:navy;"&gt;-ExpandProperty&lt;/span&gt; &lt;span style="color:blueviolet;"&gt;Paths&lt;/span&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;" class="MsoNormal"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-size:11.0pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;Script Disclaimer, for people who need to be told this sort of thing: &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;           &lt;p&gt;&lt;/p&gt;         &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;&lt;font face="Calibri"&gt;Never trust any script, including those that you find here, until you understand exactly what it does and how it will act on your systems. Always check the script on a test system or Virtual Machine, not a production system. All scripts on this site are performed by a professional stunt driver on a closed course. Your mileage may vary. Void where prohibited. Offer good for a limited time only. Keep out of reach of small children. Do not operate heavy machinery while using this script. If you experience blurry vision, indigestion or diarrhea during the operation of this script, see a physician immediately.&lt;/font&gt;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;</description></item><item><title>Tools and Processes for “Fitting it all in”</title><link>http://sqlblog.com/blogs/buck_woody/archive/2010/01/18/tools-and-processes-for-fitting-it-all-in.aspx</link><pubDate>Mon, 18 Jan 2010 14:42:05 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:21147</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p&gt;Most data professionals I’ve met work in two modes: we plan for our day, and we react to the situations around us. I’m staring at my list of things that I need to do today right now, which is my planned work. Of course, I have no idea how much of that will really get done – it’s optimistic to be sure. On the other hand I have several systems I manage, and at any moment one of them or the people that interface with them may “change state” such that I need to give them some attention.&lt;/p&gt;  &lt;p&gt;So how do I meld the two? Sometimes it can be quite difficult. I’m constantly working through my list in my mind, re-arranging what I’m focusing on based on what I perceive as the highest need. There are, however, some tools that I use each day to help me manage the workflow.&lt;/p&gt;  &lt;p&gt;I use Outlook for tracking everything, since it has a task list (my primary tracking), a calendar, mail and so on. Also I can share the information, it’s on-line so I can see it anywhere, and I can even take it offline onto the plane this week when I fly out of town. &lt;/p&gt;  &lt;p&gt;For the “ad-hoc” work, I rely on a script library, which I keep as SQL Server Management Studio projects. I keep those scripts and projects backed using Microsoft Live Mesh, which synchronizes those files (along with a few other critical files and my IE Favorites) across not only my laptop and primary systems, but even with my Virtual Machines. &lt;/p&gt;  &lt;p&gt;Also for my SQL Server systems I use the Standard Reports I’ve blogged about here. I also use Greg Larsen’s Database Dashboard, and a series of PowerShell scripts that work across my systems, alerting me to any problems. Of course I’m using SQL Server Agent Jobs quite a bit, and I also use Alerts and some Perfmon automation for my monthly baselining.&lt;/p&gt;  &lt;p&gt;So – is this your experience as well? Do you get driven by both planned and unplanned work? What tools and processes do you use to keep it all straight with your SQL Server Instances?&lt;/p&gt;</description></item><item><title>Add-Content and Out-File are not for performance</title><link>http://sqlblog.com/blogs/linchi_shea/archive/2010/01/04/add-content-and-out-file-are-not-for-performance.aspx</link><pubDate>Mon, 04 Jan 2010 21:31:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:20508</guid><dc:creator>Linchi Shea</dc:creator><description>&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;FONT size=3&gt;&lt;FONT face="Times New Roman"&gt;When you write Powershell scripts and need to write a text file, you have a number of ways to accomplish that. The often suggested approach is to use cmdlets Add-Content or Out-File. Well, this is not news.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;FONT size=3&gt;&lt;FONT face="Times New Roman"&gt;But some may not notice that these cmdlets are not there for performance. Sure, they are convenient. But if you need to write to a file many times, you may want to consider something else. To demonstrate the point, try the following script, which tests the performance of writing to a text file 10,000 times:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;# Measure Add-Content&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;$t = get-date;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;for ($a=1; $a -le 10000; $a++)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;add-content -path "c:\junk\test1.txt" -value "Hello";&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;$s = $(get-date).subtract($t).TotalMilliseconds;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;write-output ("Add-Content:&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/SPAN&gt;" + $s);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;# Measure Out-File&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;$t = get-date;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;for ($a=1; $a -le 10000; $a++)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;"Hello" | out-file -filepath "c:\junk\test2.txt" -append&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;$s = $(get-date).subtract($t).TotalMilliseconds;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;write-output ("Out-File:&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;" + $s);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;# Measure StreamWriter&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;$t = get-date;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;$sw = New-Object System.IO.StreamWriter "c:\junk\test3.txt";&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;for ($a=1; $a -le 10000; $a++)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;$sw.WriteLine("Hello")&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;$sw.Close();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;$s = $(get-date).subtract($t).TotalMilliseconds;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;write-output ("StreamWriter: " + $s);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;FONT size=3&gt;&lt;FONT face="Times New Roman"&gt;The output from this script is similar to what I obtained from my laptop and shown below:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-FAMILY:'Courier New';"&gt;&lt;FONT size=3&gt;Add-Content:&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/SPAN&gt;34015.8427&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-FAMILY:'Courier New';"&gt;&lt;FONT size=3&gt;Out-File:&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;37781.4918&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-FAMILY:'Courier New';"&gt;&lt;FONT size=3&gt;StreamWriter: 781.255&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;FONT size=3&gt;&lt;FONT face="Times New Roman"&gt;From running this scripts multiple times on my laptop, using StreamWriter was consistently about 4 ~ 5 times faster than either Add-Content or Out-File. This should not really be surprising because each time you call Add-Content or Out-File, you open and close the file, and that is expensive.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;FONT size=3&gt;&lt;FONT face="Times New Roman"&gt;Performance wise, using Add-Content or Out-File is similar to using StreamWriter in the following way (i.e. including opening and closing the file inside the loop):&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;# Measure StreamWriter, open/close the file for every write&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;$t = get-date;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;for ($a=1; $a -le 10000; $a++)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;$sw = New-Object -typename System.IO.StreamWriter("c:\junk\test4.txt", "true");&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;$sw.WriteLine("Hello")&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;$sw.Close();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;$s = $(get-date).subtract($t).TotalMilliseconds;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND:#e6e6e6;MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;FONT-SIZE:11pt;FONT-FAMILY:'Courier New';"&gt;write-output ("StreamWriter2:" + $s);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;FONT size=3&gt;&lt;FONT face="Times New Roman"&gt;And if you run&amp;nbsp;the above script, you should find its performance very similar to that of Add-Content or that of Out-File.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;FONT size=3&gt;&lt;FONT face="Times New Roman"&gt;So what is the takeaway? Well, do not use Add-Content or Out-File if performance matters such as when you write a massive number of lines to a file.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;SPAN style="FONT-WEIGHT:normal;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;</description></item></channel></rss>