<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>youdidwhatwithtsql.com &#187; Powershell Scripting</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/powershell-scripting/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Send email with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/send-email-with-powershell/889</link>
		<comments>http://www.youdidwhatwithtsql.com/send-email-with-powershell/889#comments</comments>
		<pubDate>Wed, 27 Oct 2010 19:03:18 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[Powershell Scripting]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/?p=889</guid>
		<description><![CDATA[There’s two methods I often use for sending email in my Powershell scripts. The first uses the Send-MailMessage cmdlet and the second resorts to using the .Net Framework. Why do I use two methods? Well…. ?View Code POWERSHELLSend-MailMessage -To 'me@here.com' -From 'sender@here.com' -Subject 'Test Message' -Body 'This is a test message.' -SmtpServer 'smtp.server.com'; The Send-MailMessage [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/send-email-with-powershell/889">Send email with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>There’s two methods I often use for sending email in my <a href="http://en.wikipedia.org/wiki/Windows_PowerShell" target="_blank">Powershell</a> scripts. The first uses the <a href="http://technet.microsoft.com/en-us/library/dd347693.aspx" target="_blank">Send-MailMessage</a> cmdlet and the second resorts to using the .Net Framework. Why do I use two methods? Well….</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p889code3'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8893"><td class="code" id="p889code3"><pre class="powershell" style="font-family:monospace;">Send<span style="color: pink;">-</span>MailMessage <span style="color: pink;">-</span>To <span style="color: #800000;">'me@here.com'</span> <span style="color: pink;">-</span>From <span style="color: #800000;">'sender@here.com'</span> <span style="color: pink;">-</span>Subject <span style="color: #800000;">'Test Message'</span> <span style="color: #008080; font-style: italic;">-Body</span> <span style="color: #800000;">'This is a test message.'</span> <span style="color: pink;">-</span>SmtpServer <span style="color: #800000;">'smtp.server.com'</span>;</pre></td></tr></table></div>

<p>The <a href="http://technet.microsoft.com/en-us/library/dd347693.aspx" target="_blank">Send-MailMessage</a> cmdlet is powerful and easy to use but it’s lacking one important switch. If you’re using a smtp server on a port other than the default (25) then you cannot use this cmdlet to send emails. There’s no switch available to change the port used to send email. Here’s how I get around it.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p889code4'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8894"><td class="code" id="p889code4"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$smtpClient</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Net.Mail.SmtpClient;
<span style="color: #800080;">$smtpClient</span>.Host <span style="color: pink;">=</span> <span style="color: #800000;">'smtp.server.com'</span>;
<span style="color: #800080;">$smtpClient</span>.Port <span style="color: pink;">=</span> <span style="color: #804000;">8025</span>;
<span style="color: #800080;">$smtpClient</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800000;">'email@email.com'</span><span style="color: pink;">,</span><span style="color: #800000;">'email@email.com'</span><span style="color: pink;">,</span><span style="color: #800000;">'Test Message'</span><span style="color: pink;">,</span> <span style="color: #800000;">'This is a test message.'</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p>Powershell’s ability to hook into the .Net framework allows us to get around any limitation existing in the built in <a href="http://msdn.microsoft.com/en-us/library/ms714395(VS.85).aspx" target="_blank">cmdlets</a>. However, I’m still rooting for a –Port switch in the next version of Powershell!</p>
<p><map name='google_ad_map_889_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/889?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_889_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=889&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fsend-email-with-powershell%2F889' title="Send email with Powershell" alt=" Send email with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/send-email-with-powershell/889">Send email with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/send-email-with-powershell/889" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/send-email-with-powershell/889/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Console input with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/console-input-with-powershell/673</link>
		<comments>http://www.youdidwhatwithtsql.com/console-input-with-powershell/673#comments</comments>
		<pubDate>Sun, 21 Feb 2010 19:51:07 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Powershell Scripting]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/console-input-with-powershell/673</guid>
		<description><![CDATA[I&#8217;m looking at building some Powershell scripts that can accept user input to perform different tasks with a wizard style interface. As it happens this is fairly easily achieved with the Read-Host cmdlet. Here&#8217;s a quick script showing how such a powershell script may look. ?View Code POWERSHELLfunction mainMenu&#40;&#41; &#123; Clear-Host; Write-Host &#34;============&#34;; Write-Host &#34;= [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/console-input-with-powershell/673">Console input with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m looking at building some <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> scripts that can accept user input to perform different tasks with a wizard style interface. As it happens this is fairly easily achieved with the <a href="http://technet.microsoft.com/en-us/library/ee176935.aspx" target="_blank">Read-Host</a> cmdlet. Here&#8217;s a quick script showing how such a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">powershell</a> script may look.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p673code6'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6736"><td class="code" id="p673code6"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> mainMenu<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	Clear<span style="color: pink;">-</span>Host;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;============&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;= MAINMENU =&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;============&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;1. Press '1' for this option&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;2. Press '2' for this option&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;3. Press '3' for this option&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;4. Press '4' for this option&quot;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">function</span> returnMenu<span style="color: #000000;">&#40;</span><span style="color: #800080;">$option</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	Clear<span style="color: pink;">-</span>Host;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;You chose option $option&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Press any key to return to the main menu.&quot;</span>;
	<span style="color: #800080;">$host</span>.UI.RawUI.ReadKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;NoEcho,IncludeKeyDown&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">do</span>
<span style="color: #000000;">&#123;</span>
	mainMenu;
	<span style="color: #800080;">$input</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Read-Host</span> <span style="color: #800000;">&quot;Enter a number for an option or type <span style="color: #008080; font-weight: bold;">`&quot;</span>quit<span style="color: #008080; font-weight: bold;">`&quot;</span> to finish.&quot;</span>
	<span style="color: #0000FF;">switch</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$input</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800000;">&quot;1&quot;</span>
		<span style="color: #000000;">&#123;</span>
			returnMenu <span style="color: #800080;">$input</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;2&quot;</span>
		<span style="color: #000000;">&#123;</span>
			returnMenu <span style="color: #800080;">$input</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;3&quot;</span>
		<span style="color: #000000;">&#123;</span>
			returnMenu <span style="color: #800080;">$input</span>;		
		<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;4&quot;</span>
		<span style="color: #000000;">&#123;</span>
			returnMenu <span style="color: #800080;">$input</span>;		
		<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;quit&quot;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008000;"># nothing</span>
		<span style="color: #000000;">&#125;</span>
		default
		<span style="color: #000000;">&#123;</span>
			Clear<span style="color: pink;">-</span>Host;
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Invalid input. Please enter a valid option. Press any key to continue.&quot;</span>;
			<span style="color: #800080;">$host</span>.UI.RawUI.ReadKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;NoEcho,IncludeKeyDown&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span> <span style="color: #0000FF;">until</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$input</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;quit&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
Clear<span style="color: pink;">-</span>Host;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/powershell_console_input_menu.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="powershell console input menu" border="0" alt="powershell console input menu thumb Console input with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/powershell_console_input_menu_thumb.png" width="644" height="350" /></a> </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/powershell_console_input_menu_1.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="powershell console input menu 1" border="0" alt="powershell console input menu 1 thumb Console input with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/powershell_console_input_menu_1_thumb.png" width="644" height="350" /></a></p>
<p><map name='google_ad_map_673_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/673?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_673_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=673&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fconsole-input-with-powershell%2F673' title="Console input with Powershell" alt=" Console input with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/console-input-with-powershell/673">Console input with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/console-input-with-powershell/673" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/console-input-with-powershell/673/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Managing Index Fragmentation with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622</link>
		<comments>http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622#comments</comments>
		<pubDate>Tue, 09 Feb 2010 22:24:41 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[index fragmentation]]></category>
		<category><![CDATA[indexes]]></category>
		<category><![CDATA[Powershell Scripting]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622</guid>
		<description><![CDATA[Here&#8217;s a Powershell script that can be used to manage index fragmentation in SQL Server databases. The strategy I&#8217;ve used in the script is based on a recommendation from Pinal Dave (blog &#124; twitter) in his article&#160; Difference Between Index Rebuild and Index Reorganize Explained with T-SQL Script. Just set the $sqlserver and $database variables [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622">Managing Index Fragmentation with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script that can be used to manage index fragmentation in <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> databases. The strategy I&#8217;ve used in the script is based on a recommendation from Pinal Dave (<a href="http://blog.sqlauthority.com" target="_blank">blog</a> | <a href="http://twitter.com/pinaldave" target="_blank">twitter</a>) in his article&nbsp; <a href="http://blog.sqlauthority.com/2007/12/22/sql-server-difference-between-index-rebuild-and-index-reorganize-explained-with-t-sql-script/" target="_blank">Difference Between Index Rebuild and Index Reorganize Explained with T-SQL Script</a>. Just set the <strong>$sqlserver </strong>and <strong>$database </strong>variables to something appropriate for your environment. Enjoy!</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p622code8'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6228"><td class="code" id="p622code8"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Load SMO</span>
<span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Reflection.Assembly</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadWithPartialName</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">'Microsoft.SqlServer.SMO'</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>;
&nbsp;
<span style="color: #008000;"># Set sql server and database name here</span>
<span style="color: #800080;">$sqlserver</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost\sql2005&quot;</span>;
<span style="color: #800080;">$database</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;AdventureWorks&quot;</span>;
&nbsp;
<span style="color: #800080;">$srv</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #800000;">&quot;Microsoft.SqlServer.Management.SMO.Server&quot;</span> <span style="color: #800080;">$sqlserver</span>;
<span style="color: #800080;">$db</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Microsoft.SqlServer.Management.SMO.Database&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$db</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Databases<span style="color: #000000;">&#91;</span><span style="color: #800080;">$database</span><span style="color: #000000;">&#93;</span>;
&nbsp;
<span style="color: #008000;"># Get table count</span>
<span style="color: #800080;">$table_count</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.Tables.Count;
<span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
&nbsp;
<span style="color: #008000;"># First script out the drops</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$table</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$db</span>.Tables<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Checking table $table&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">/</span> <span style="color: #800080;">$table_count</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing indexes&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
	<span style="color: #800080;">$i</span><span style="color: pink;">++</span>;
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$index</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$table</span>.Indexes<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$index_name</span> <span style="color: pink;">=</span> <span style="color: #800080;">$index</span>.Name;
		<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Checking table $table&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">/</span> <span style="color: #800080;">$table_count</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing index $index_name&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
		<span style="color: #008000;"># Get the fragmentation stats</span>
		<span style="color: #800080;">$frag_stats</span> <span style="color: pink;">=</span> <span style="color: #800080;">$index</span>.EnumFragmentation<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #008000;"># Get the properties we need to work with the index</span>
		<span style="color: #800080;">$frag_stats</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">ForEach-Object</span> <span style="color: #000000;">&#123;</span>
						<span style="color: #800080;">$Index_Name</span> <span style="color: pink;">=</span> <span style="color: #000080;">$_</span>.Index_Name;
						<span style="color: #800080;">$Index_Type</span> <span style="color: pink;">=</span> <span style="color: #000080;">$_</span>.Index_Type;
						<span style="color: #800080;">$Average_Fragmentation</span> <span style="color: pink;">=</span> <span style="color: #000080;">$_</span>.AverageFragmentation;
									<span style="color: #000000;">&#125;</span>;
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Green <span style="color: #800000;">&quot;$Index_Type $Index_Name has a fragmentation percentage of $Average_Fragmentation&quot;</span>;
&nbsp;
		<span style="color: #008000;"># Here we decide what to do based on the level on fragmentation</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Average_Fragmentation</span> <span style="color: #FF0000;">-gt</span> <span style="color: #804000;">40.00</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Red <span style="color: #800000;">&quot;$Index_Name is more than 40% fragmented and will be rebuilt.&quot;</span>;
			<span style="color: #800080;">$index</span>.Rebuild<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Green <span style="color: #800000;">&quot;$Index_Name has been rebuilt.&quot;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">elseif</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Average_Fragmentation</span> <span style="color: #FF0000;">-ge</span> <span style="color: #804000;">10.00</span> <span style="color: #FF0000;">-and</span> <span style="color: #800080;">$Average_Fragmentation</span> <span style="color: #FF0000;">-le</span> <span style="color: #804000;">40.00</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Red <span style="color: #800000;">&quot;$Index_Name is between 10-40% fragmented and will be reorganized.&quot;</span>;
			<span style="color: #800080;">$index</span>.Reorganize<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Green <span style="color: #800000;">&quot;$Index_Name has been reorganized.&quot;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">else</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Red <span style="color: #800000;">&quot;$Index_Name is healthy, with $Average_Fragmentation% fragmentation, and will be left alone.&quot;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Finished processing <span style="color: #008080; font-weight: bold;">`&quot;</span>$database<span style="color: #008080; font-weight: bold;">`&quot;</span> indexes.&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #804000;">100</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Done&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Seconds</span> <span style="color: #804000;">2</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/AdventureWork_Index_Management.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="AdventureWork Index Management with Powershell" border="0" alt="AdventureWork Index Management thumb Managing Index Fragmentation with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/AdventureWork_Index_Management_thumb.png" width="644" height="373" /></a></p>
<p><map name='google_ad_map_622_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/622?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_622_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=622&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fmanaging-index-fragmentation-with-powershell%2F622' title="Managing Index Fragmentation with Powershell" alt=" Managing Index Fragmentation with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622">Managing Index Fragmentation with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Powershell Random sort</title>
		<link>http://www.youdidwhatwithtsql.com/powershell-random-sort/596</link>
		<comments>http://www.youdidwhatwithtsql.com/powershell-random-sort/596#comments</comments>
		<pubDate>Mon, 01 Feb 2010 11:56:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[random sort]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/powershell-random-sort/596</guid>
		<description><![CDATA[Here&#8217;s a Powershell snippet that randomly sorts the lines in a file. The snippet below reads a text file, called file.txt, located in your user profile directory. The data in the file will be written back with the lines in a different order. ?View Code POWERSHELL$data = New-Object System.Object; $data = Get-Content &#34;$Env:USERPROFILE\file.txt&#34;; # Random [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/powershell-random-sort/596">Powershell Random sort</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> snippet that randomly sorts the lines in a file. The snippet below reads a text file, called <strong>file.txt</strong>, located in your user profile directory. The data in the file will be written back with the lines in a different order.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p596code11'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p59611"><td class="code" id="p596code11"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$data</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Object;
<span style="color: #800080;">$data</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\file.txt&quot;</span>;
<span style="color: #008000;"># Random sort the lines in the file</span>
<span style="color: #800080;">$data</span> <span style="color: pink;">=</span> <span style="color: #800080;">$data</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">sort</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#91;</span>System.Guid<span style="color: #000000;">&#93;</span>::NewGuid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span>;
<span style="color: #008080; font-weight: bold;">Set-Content</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\file.txt&quot;</span> <span style="color: #800080;">$data</span>;</pre></td></tr></table></div>

<p>If your file started off like this&#8230;</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/file_ordered.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="file ordered" border="0" alt="file ordered thumb Powershell Random sort" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/file_ordered_thumb.png" width="244" height="208" /></a> </p>
</p>
<p>It will end up looking something like&#8230;</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/file_random_sort1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="file random sort" border="0" alt="file random sort thumb Powershell Random sort" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/file_random_sort_thumb.png" width="244" height="234" /></a> </p>
<p>This is functionally equivalent to&#8230;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p596code12'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p59612"><td class="code" id="p596code12"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Customers</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> NEWID<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p><map name='google_ad_map_596_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/596?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_596_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=596&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fpowershell-random-sort%2F596' title="Powershell Random sort" alt=" Powershell Random sort" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/powershell-random-sort/596">Powershell Random sort</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/powershell-random-sort/596" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/powershell-random-sort/596/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using .Net libraries in Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/using-net-libraries-in-powershell/538</link>
		<comments>http://www.youdidwhatwithtsql.com/using-net-libraries-in-powershell/538#comments</comments>
		<pubDate>Sat, 16 Jan 2010 16:50:44 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Powershell Scripting]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/using-net-libraries-in-powershell/538</guid>
		<description><![CDATA[One of the great things about Powershell is its ability to take advantage of the .Net platform. If Powershell can&#8217;t do it you bet there&#8217;s a .Net library that can. Not only is this great for extending your scripts but I also like to use Powershell to test some of my classes. Rather than fire [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/using-net-libraries-in-powershell/538">Using .Net libraries in Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>One of the great things about <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> is its ability to take advantage of the <a href="http://www.microsoft.com/NET/" target="_blank">.Net platform</a>. If Powershell can&#8217;t do it you bet there&#8217;s a .Net library that can. Not only is this great for extending your scripts but I also like to use Powershell to test some of my classes. Rather than fire up RAM hungry Visual Studio I can just script out a few tests in <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a>.</p>
<p>Here&#8217;s quick script you can modify to compile a .cs file to produce a library for Powershell to load. This gives me the flexibility to quickly test individual classes from a project. The class I was testing here is just a wrapper around the functions of <a href="http://www.codeplex.com/DotNetZip" target="_blank">DotNetZip</a>. A reference is included, just remove this if your class doesn&#8217;t need it. At the end of the script the .dll file will be loaded and its methods will be listed by a call to <a href="http://technet.microsoft.com/en-us/library/ee176854.aspx" target="_blank">Get-Member</a>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p538code15'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p53815"><td class="code" id="p538code15"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Your c# class we want to compile as a library to test</span>
<span style="color: #800080;">$csharp_file</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`&quot;</span>C:\Users\Rhys\Documents\Visual Studio 2008\Projects\ETLBuddy\ETLBuddy\Zipper.cs<span style="color: #008080; font-weight: bold;">`&quot;</span>&quot;</span>;
<span style="color: #008000;"># Path to csc compiler</span>
<span style="color: #800080;">$compiler</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$env:windir\Microsoft.NET\Framework\v3.5\csc.exe&quot;</span>;
<span style="color: #800080;">$cmd</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$compiler /target:library $csharp_file &quot;</span>
<span style="color: #008000;"># Add any external library you're referencing, empty string if none</span>
<span style="color: #800080;">$reference</span><span style="color: pink;">=</span><span style="color: #800000;">&quot;C:\Users\Rhys\Desktop\Ionic.Zip.dll&quot;</span>;
&nbsp;
<span style="color: #008000;"># Add reference if needed</span>
<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$reference</span> <span style="color: #FF0000;">-ne</span> <span style="color: #800000;">&quot;&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$cmd</span> <span style="color: pink;">+=</span> <span style="color: #800000;">&quot; /r:<span style="color: #008080; font-weight: bold;">`&quot;</span>$reference<span style="color: #008080; font-weight: bold;">`&quot;</span>&quot;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Invoke-Expression tells ps to evaluate the string as a command</span>
<span style="color: #008000;"># otherwise it would just print it out</span>
<span style="color: #008080; font-weight: bold;">Invoke-Expression</span> <span style="color: #800080;">$cmd</span>;
&nbsp;
<span style="color: #008000;"># Now load the dll</span>
<span style="color: #800080;">$dll</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Location</span>;
<span style="color: #800080;">$dll</span> <span style="color: pink;">=</span> <span style="color: #800080;">$dll</span>.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$dll</span> <span style="color: pink;">=</span> <span style="color: #800080;">$dll</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;\&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$csharp_file</span>.Substring<span style="color: #000000;">&#40;</span><span style="color: #800080;">$csharp_file</span>.LastIndexOf<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;\&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">+</span> <span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;.cs&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;.dll&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`&quot;</span>&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #000000;">&#91;</span>Reflection.Assembly<span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadFile</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$dll</span> <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;.cs&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;.dll&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008000;"># Create a new object from the loaded dll</span>
<span style="color: #800080;">$obj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #800000;">&quot;com.etlbuddy.ETLBuddy.Zipper&quot;</span>;
<span style="color: #800080;">$obj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Get-Member</span>;</pre></td></tr></table></div>

<p>From the output here you can see there are two methods called <strong>Zip</strong> and <strong>Unzip</strong>.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/powershell_net_library.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="powershell .net library methods" border="0" alt="powershell net library thumb Using .Net libraries in Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/powershell_net_library_thumb.png" width="644" height="350" /></a> </p>
<p>Now I have a dll I can put to work in my Powershell Scripts! Once this is done it&#8217;s really quite simple to get started.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p538code16'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p53816"><td class="code" id="p538code16"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Load the Zipper dll</span>
<span style="color: #008000;"># Needed to run gacutil /i &quot;C:\Users\Rhys\Desktop\Ionic.Zip.dll&quot; so this would work</span>
<span style="color: #000000;">&#91;</span>Reflection.Assembly<span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadFile</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;C:\Users\Rhys\Documents\powershell\Zipper.dll&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>;
&nbsp;
<span style="color: #008000;"># Create a new zip object</span>
<span style="color: #800080;">$obj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #800000;">&quot;com.etlbuddy.ETLBuddy.Zipper&quot;</span>;
&nbsp;
<span style="color: #008000;"># Zip a copy of a library</span>
<span style="color: #800080;">$obj</span>.Zip<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;C:\Users\Rhys\Documents\powershell\Zipper.dll&quot;</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p>After execution I have a new zip file in my Powershell working directory.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/file_zipped_by_powershell.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="file zipped by powershell" border="0" alt="file zipped by powershell thumb Using .Net libraries in Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/file_zipped_by_powershell_thumb.png" width="644" height="185" /></a> </p>
<p>That&#8217;s really all there is to begin using an external library in your Powershell scripts. For further info checkout <a href="http://blogs.msdn.com/powershell/archive/2009/03/11/how-to-create-an-object-in-powershell.aspx" target="_blank">How to create an object in Powershell</a>.</p>
<p><map name='google_ad_map_538_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/538?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_538_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=538&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fusing-net-libraries-in-powershell%2F538' title="Using .Net libraries in Powershell" alt=" Using .Net libraries in Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/using-net-libraries-in-powershell/538">Using .Net libraries in Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/using-net-libraries-in-powershell/538" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/using-net-libraries-in-powershell/538/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Discover SQL Servers with Powershell via the registry</title>
		<link>http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell-via-the-registry/494</link>
		<comments>http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell-via-the-registry/494#comments</comments>
		<pubDate>Mon, 28 Dec 2009 15:26:11 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell-via-the-registry/494</guid>
		<description><![CDATA[Chuck Boyce Jr (blog &#124; twitter) recently commented on a limitation of the script from my post Discover SQL Servers with Powershell. The script does require that the SQLBrowser service is running for discovery to occur which may be a major issue for some. Here&#8217;s an alternative method that does not have this limitation. All [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell-via-the-registry/494">Discover SQL Servers with Powershell via the registry</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Chuck Boyce Jr (<a href="http://chuckboyce.blogspot.com/" target="_blank">blog</a> | <a href="http://twitter.com/chuckboycejr" target="_blank">twitter</a>) recently <a href="http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell/357/comment-page-1#comment-120" target="_blank">commented</a> on a limitation of the script from my post <a href="http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell/357" target="_blank">Discover SQL Servers with Powershell</a>. The script does require that the <a href="http://msdn.microsoft.com/en-us/library/ms181087.aspx" target="_blank">SQLBrowser</a> service is running for discovery to occur which may be a major issue for some. Here&#8217;s an alternative method that does not have this limitation. All <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> instances should have their name registered in the following registry key <strong>HKLM\Software\Microsoft\Microsoft SQL Server\InstalledInstances</strong> which we can access remotely<strong> </strong>with <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a>. </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/registry_installedinstances.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="The INSTALLEDINSTANCES registry key showing SQL Servers" border="0" alt="registry installedinstances thumb Discover SQL Servers with Powershell via the registry" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/registry_installedinstances_thumb.png" width="244" height="150" /></a> </p>
<p>All this script requires is that you place a text file containing computer names in your user profile folder (C:\Users\Rhys on my laptop).</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p494code18'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p49418"><td class="code" id="p494code18"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Text file containing computers to search for sql instances</span>
<span style="color: #800080;">$computers</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;$env:USERPROFILE\computers.txt&quot;</span>;
&nbsp;
<span style="color: #008000;"># Check each computer for installed SQL Server</span>
<span style="color: #008000;"># instances by searching the registry</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$computer</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$computers</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	try
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$sql</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Microsoft.Win32.RegistryKey<span style="color: #000000;">&#93;</span>::OpenRemoteBaseKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'LocalMachine'</span><span style="color: pink;">,</span><span style="color: #800080;">$computer</span><span style="color: #000000;">&#41;</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'SOFTWARE\Microsoft\Microsoft SQL Server'</span><span style="color: #000000;">&#41;</span>.GetValue<span style="color: #000000;">&#40;</span><span style="color: #800000;">'InstalledInstances'</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$sqlserver</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$sql</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$sqlserver</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;MSSQLSERVER&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;"># Default instance</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Green <span style="color: #800000;">&quot;$computer (Default instance)&quot;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0000FF;">else</span> <span style="color: #008000;"># Named instance</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Green <span style="color: #800000;">&quot;$computer\$sqlserver (Named instance)&quot;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	catch <span style="color: #000000;">&#91;</span>System.Exception<span style="color: #000000;">&#93;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Red <span style="color: #800000;">&quot;Error accessing $computer. &quot;</span> <span style="color: #000080;">$_</span>.Exception.Message;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>When you execute the script it will search each computer and list the SQL Server instances it discovered in the registry.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/discovered_sql_servers.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="discovered sql servers" border="0" alt="discovered sql servers thumb Discover SQL Servers with Powershell via the registry" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/discovered_sql_servers_thumb.png" width="244" height="134" /></a> </p>
<p>If you receive the error &quot;You cannot call a method on a null-valued expression&quot; then that computer does not have the registry key we are searching for. In other words; no SQL Server instances are on that computer. Hopefully this method should allow you to get a more complete view of the SQL Servers in your organisation. </p>
<p><map name='google_ad_map_494_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/494?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_494_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=494&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fdiscover-sql-servers-with-powershell-via-the-registry%2F494' title="Discover SQL Servers with Powershell via the registry" alt=" Discover SQL Servers with Powershell via the registry" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell-via-the-registry/494">Discover SQL Servers with Powershell via the registry</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell-via-the-registry/494" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell-via-the-registry/494/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Converting CSV FileS to XML with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408</link>
		<comments>http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408#comments</comments>
		<pubDate>Wed, 28 Oct 2009 21:34:58 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408</guid>
		<description><![CDATA[Powershell is a pretty cool tool for many things including working with data. It’s just such a great time saver if you have to deal with multiple files or need to change them into different formats. Here’s how easy it is to turn a csv file into well-formed xml. ?View Code POWERSHELL# csv file to [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408">Converting CSV FileS to XML with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> is a pretty cool tool for many things including working with data. It’s just such a great time saver if you have to deal with multiple files or need to change them into different formats. Here’s how easy it is to turn a csv file into well-formed xml.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p408code22'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p40822"><td class="code" id="p408code22"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># csv file to convert</span>
<span style="color: #800080;">$csv</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\Users\Rhys\Desktop\csv\file1.csv&quot;</span>;
<span style="color: #008000;"># xml file to create</span>
<span style="color: #800080;">$xml</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\Users\Rhys\Desktop\csv\file1.xml&quot;</span>;
&nbsp;
<span style="color: #008080; font-weight: bold;">Import-Csv</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$csv</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Export-Clixml</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$xml</span>;</pre></td></tr></table></div>

<p>This will produce xml looking something like this.</p>
<p>&lt;?XML:NAMESPACE PREFIX = [default] http://schemas.microsoft.com/powershell/2004/04 NS = &quot;http://schemas.microsoft.com/powershell/2004/04&quot; /&gt;&lt;?XML:NAMESPACE PREFIX = [default] http://schemas.microsoft.com/powershell/2004/04 NS = &quot;http://schemas.microsoft.com/powershell/2004/04&quot; /&gt;&lt;objs xmlns=&quot;http://schemas.microsoft.com/powershell/2004/04&quot; version=&quot;1.1.0.1&quot;&gt;<br />
  &lt;obj refid=&quot;0&quot;&gt;<br />
    &lt;tn refid=&quot;0&quot;&gt;<br />
      &lt;t&gt;System.Management.Automation.PSCustomObject&lt;/t&gt;<br />
      &lt;t&gt;System.Object&lt;/t&gt;<br />
    &lt;/tn&gt;<br />
    &lt;ms&gt;<br />
      &lt;s n=&quot;FirstName&quot;&gt;Rhys&lt;/s&gt;<br />
      &lt;s n=&quot;LastName&quot;&gt;Campbell&lt;/s&gt;<br />
      &lt;s n=&quot;Age&quot;&gt;29&lt;/s&gt;<br />
    &lt;/ms&gt;<br />
  &lt;/obj&gt;<br />
  &lt;obj refid=&quot;1&quot;&gt;<br />
    &lt;tnref refid=&quot;0&quot;&gt;&lt;/tnref&gt;<br />
    &lt;ms&gt;<br />
      &lt;s n=&quot;FirstName&quot;&gt;Joe&lt;/s&gt;<br />
      &lt;s n=&quot;LastName&quot;&gt;Bloggs&lt;/s&gt;<br />
      &lt;s n=&quot;Age&quot;&gt;40&lt;/s&gt;<br />
    &lt;/ms&gt;<br />
  &lt;/obj&gt;<br />
  &lt;obj refid=&quot;2&quot;&gt;<br />
    &lt;tnref refid=&quot;0&quot;&gt;&lt;/tnref&gt;<br />
    &lt;ms&gt;<br />
      &lt;s n=&quot;FirstName&quot;&gt;Steve&lt;/s&gt;<br />
      &lt;s n=&quot;LastName&quot;&gt;Smith&lt;/s&gt;<br />
      &lt;s n=&quot;Age&quot;&gt;35&lt;/s&gt;<br />
    &lt;/ms&gt;<br />
  &lt;/obj&gt;<br />
&lt;/objs&gt;</p>
<p>With the inclusion of the <a href="http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/get-childitem.mspx" target="_blank">Get-ChildItem cmdlet</a> we can merge multiple csv files into a single xml file with just a few lines of code.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p408code23'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p40823"><td class="code" id="p408code23"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Folder of csv files</span>
<span style="color: #800080;">$csvFiles</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;C:\Users\Rhys\Desktop\csv\*&quot;</span> <span style="color: #008080; font-style: italic;">-Include</span> <span style="color: pink;">*</span>.csv;
&nbsp;
<span style="color: #008000;"># Process each csv file. Need to be the same structure</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$file</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$csvFiles</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$csvContent</span> <span style="color: pink;">+=</span> <span style="color: #008080; font-weight: bold;">Import-Csv</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$file</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Export the imported data as one xml file</span>
<span style="color: #800080;">$csvContent</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Export-Clixml</span> <span style="color: #008080; font-style: italic;">-Path</span> C:\Users\Rhys\Desktop\csv\merged.xml;</pre></td></tr></table></div>

<p>Turning this back into a csv file is a simple <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> one-liner!</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p408code24'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p40824"><td class="code" id="p408code24"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Turn this back into csv</span>
<span style="color: #008080; font-weight: bold;">Import-Clixml</span> <span style="color: #008080; font-style: italic;">-Path</span> C:\Users\Rhys\Desktop\csv\merged.xml <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Export-Csv</span> <span style="color: #008080; font-style: italic;">-Path</span> C:\Users\Rhys\Desktop\csv\BackToCsv.csv <span style="color: #008080; font-style: italic;">-NoTypeInformation</span>;</pre></td></tr></table></div>

<p>Check out my other data related Powershell posts; <a href="http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374" target="_blank">splitting csv files with Powershell</a>, <a href="http://www.youdidwhatwithtsql.com/merging-csv-files-with-powershell/330" target="_blank">merging csv files with Powershell</a> and <a href="http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388" target="_blank">trimming whitespace with Powershell</a>.</p>
<p><map name='google_ad_map_408_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/408?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_408_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=408&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fconverting-csv-files-to-xml-with-powershell%2F408' title="Converting CSV FileS to XML with Powershell" alt=" Converting CSV FileS to XML with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408">Converting CSV FileS to XML with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Trimming Whitespace with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388</link>
		<comments>http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388#comments</comments>
		<pubDate>Thu, 01 Oct 2009 19:14:11 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[whitespace]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388</guid>
		<description><![CDATA[A few days ago I was working with a client that was providing an export of data from Oracle. The file being produced was choking my SSIS package due to various formatting issues. After working with the client and getting a file that looked good to the naked eye I discovered that a large amount [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388">Trimming Whitespace with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>A few days ago I was working with a client that was providing an export of data from <a href="http://www.oracle.com/index.html" target="_blank">Oracle</a>. The file being produced was choking my <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> package due to various formatting issues. After working with the client and getting a file that looked good to the naked eye I discovered that a large amount of whitespace on the end of each line was making things break at my end.</p>
<p>I need to import this file quickly so I decided to fix it myself. As usual <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> was up to the task! Here’s the script I came up with. This script will process a file called <strong>test.csv</strong> located in e:\ and produce a new file called <strong>test2.csv</strong> with the trailing whitespace removed from each line.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p388code26'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p38826"><td class="code" id="p388code26"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$content</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span>;
<span style="color: #800080;">$file</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;e:\test.csv&quot;</span>;
<span style="color: #800080;">$count</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Processing file&quot;</span> <span style="color: #008080; font-style: italic;">-CurrentOperation</span> <span style="color: #800000;">&quot;Line = 0&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #804000;">0</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Starting&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
<span style="color: #800080;">$lines</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$file</span>;
<span style="color: #800080;">$percent_complete</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
&nbsp;
<span style="color: #008000;"># trim line by line</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$line</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$lines</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
 	<span style="color: #800080;">$line</span> <span style="color: pink;">=</span> <span style="color: #800080;">$line</span>.TrimEnd<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
 	<span style="color: #800080;">$content</span> <span style="color: pink;">+=</span> <span style="color: #800000;">&quot;$line<span style="color: #008080; font-weight: bold;">`n</span>&quot;</span> <span style="color: #008000;"># Add a newline</span>
 	<span style="color: #800080;">$count</span><span style="color: pink;">++</span>;
 	<span style="color: #800080;">$percent_complete</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Ceiling<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$count</span> <span style="color: pink;">/</span> <span style="color: #800080;">$lines</span>.Count<span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
	 <span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Processing file&quot;</span> <span style="color: #008080; font-style: italic;">-CurrentOperation</span> <span style="color: #800000;">&quot;Line = $count&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #800080;">$percent_complete</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Running&quot;</span>  <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #800080;">$content</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Set-Content</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;e:\test2.csv&quot;</span>;
<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Finished processing file&quot;</span> <span style="color: #008080; font-style: italic;">-CurrentOperation</span> <span style="color: #800000;">&quot;All lines processed&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #804000;">100</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Complete&quot;</span>  <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
<span style="color: #008080; font-weight: bold;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">5</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/trim_whitespace_from_files_powershell.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="trim_whitespace_from_files_powershell" border="0" alt="trim whitespace from files powershell thumb Trimming Whitespace with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/trim_whitespace_from_files_powershell_thumb.png" width="244" height="134" /></a> </p>
<p>After this quick fix the file was easily imported!</p>
<p><map name='google_ad_map_388_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/388?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_388_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=388&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Ftrimming-whitespace-with-powershell%2F388' title="Trimming Whitespace with Powershell" alt=" Trimming Whitespace with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388">Trimming Whitespace with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Splitting csv files with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374</link>
		<comments>http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374#comments</comments>
		<pubDate>Wed, 23 Sep 2009 21:59:21 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[Powershell Scripting]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374</guid>
		<description><![CDATA[I’ve blogged before about the usefulness of Powershell for data tasks. A few weeks ago I had a requirement at work for merging csv files and recently I needed to split a single csv file into several files. While this is easy to do using SSIS and a bit of T-SQL it is a little [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374">Splitting csv files with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I’ve blogged before about the usefulness of <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> for data tasks. A few weeks ago I had a requirement at work for <a href="http://www.youdidwhatwithtsql.com/merging-csv-files-with-powershell/330" target="_blank">merging csv files</a> and recently I needed to split a single csv file into several files.</p>
<p>While this is easy to do using <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> and a bit of <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> it is a little tedious, and more time consuming, that it needs to be for adhoc jobs. Again Powershell came to the rescue! Here’s an example.</p>
<p>First I created a csv called bigCsvfile.csv, with 1, 000 records of data and placed it into my user profile directory; <strong>C:\Users\Rhys</strong> on my laptop. </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/splitting_csv_files_with_powershell_1.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="splitting_csv_files_with_powershell_1" border="0" alt="splitting csv files with powershell 1 thumb Splitting csv files with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/splitting_csv_files_with_powershell_1_thumb.png" width="169" height="244" /></a> </p>
</p>
<p>This <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script does require a sequential integer id for each record to work correctly as it uses this to divide up the records. Now assuming you have a csv file, called <strong>bigCsvFile.csv</strong>,<strong> </strong>in the correct location then the only thing you should need to change is the <strong>$split </strong>variable value. This is now many files you would like to split the original file into.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p374code28'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37428"><td class="code" id="p374code28"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Author: Rhys Campbell</span>
<span style="color: #008000;"># 2009-09-22</span>
<span style="color: #008000;"># Splitting up csv files</span>
&nbsp;
<span style="color: #008000;"># Path to csv file. Must contain a sequential unique integer id column </span>
<span style="color: #800080;">$csvFile</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\bigcsvFile.csv&quot;</span>;
<span style="color: #008000;"># Slit into how many files?</span>
<span style="color: #800080;">$split</span> <span style="color: pink;">=</span> <span style="color: #804000;">10</span>;
&nbsp;
<span style="color: #008000;"># Get the csv file content</span>
<span style="color: #800080;">$content</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Import-Csv</span> <span style="color: #800080;">$csvFile</span>;
&nbsp;
<span style="color: #008000;"># So we start from Id = 1 in the csv file</span>
<span style="color: #800080;">$start</span> <span style="color: pink;">=</span> <span style="color: #804000;">1</span>;
<span style="color: #800080;">$end</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
&nbsp;
<span style="color: #008000;"># calc records per file</span>
<span style="color: #800080;">$records_per_file</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Ceiling<span style="color: #000000;">&#40;</span><span style="color: #800080;">$content</span>.Count <span style="color: pink;">/</span> <span style="color: #800080;">$split</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #804000;">1</span>; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-le</span> <span style="color: #800080;">$split</span>; <span style="color: #800080;">$i</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># Set the end value for selecting records</span>
	<span style="color: #800080;">$end</span> <span style="color: pink;">+=</span> <span style="color: #800080;">$records_per_file</span>;
	<span style="color: #008000;"># Need to cast to int or we get an alphabetic comparison when we want a numeric one</span>
	<span style="color: #800080;">$content</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #000080;">$_</span>.Id <span style="color: #FF0000;">-ge</span> <span style="color: #800080;">$start</span> <span style="color: #FF0000;">-and</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #000080;">$_</span>.Id <span style="color: #FF0000;">-le</span> <span style="color: #800080;">$end</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Export-Csv</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\file$i.csv&quot;</span> <span style="color: #008080; font-style: italic;">-NoTypeInformation</span>;
	<span style="color: #008000;"># Update start value for selecting records</span>
	<span style="color: #800080;">$start</span> <span style="color: pink;">=</span> <span style="color: #800080;">$end</span> <span style="color: pink;">+</span> <span style="color: #804000;">1</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Once the script has executed successfully it will create the split csv files in your user profile folder. The new files are named sequentially, i.e. file1.csv, file2.csv, file3.csv etc.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/split_csv_files.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="split_csv_files" border="0" alt="split csv files thumb Splitting csv files with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/split_csv_files_thumb.png" width="244" height="150" /></a> </p>
<p>Depending on the number of records, and the number of files split to, the records may not be the same in each file. Provided the number of records versus file split is reasonable it will be pretty close. Now with a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script it takes just a few moments to perform those previously tedious tasks!</p>
<p><map name='google_ad_map_374_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/374?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_374_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=374&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fsplitting-csv-files-with-powershell%2F374' title="Splitting csv files with Powershell" alt=" Splitting csv files with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374">Splitting csv files with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Progress Bars within the Powershell Console</title>
		<link>http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366</link>
		<comments>http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366#comments</comments>
		<pubDate>Tue, 15 Sep 2009 20:14:35 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[Progress Bars]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366</guid>
		<description><![CDATA[Progress bars can be a nice visual indicator as to how a far a task is into its workload. Windows Powershell provides us with the ability to create these within the console fairly easily. This simple code will demonstrate the basics of the Write-Progress cmdlet, which allows us to deploy progress bars in our scripts. [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366">Using Progress Bars within the Powershell Console</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Progress bars can be a nice visual indicator as to how a far a task is into its workload. Windows <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> provides us with the ability to create these within the console fairly easily.</p>
<p>This simple code will demonstrate the basics of the <a href="http://technet.microsoft.com/en-us/library/dd347663.aspx" target="_blank">Write-Progress</a> <a href="http://msdn.microsoft.com/en-us/library/ms714395(VS.85).aspx" target="_blank">cmdlet</a>, which allows us to deploy progress bars in our scripts.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p366code32'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p36632"><td class="code" id="p366code32"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-le</span> <span style="color: #804000;">100</span>; <span style="color: #800080;">$i</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Activity&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #800080;">$i</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #804000;">100</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Progress bar within the Powershell Console" border="0" alt="image thumb Using Progress Bars within the Powershell Console" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/image_thumb.png" width="244" height="127" /></a> </p>
<p>It’s possible to spawn multiple progress bars within the Console. This may be handy for displaying the advance of child tasks. Here’s a simple example.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p366code33'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p36633"><td class="code" id="p366code33"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-le</span> <span style="color: #804000;">100</span>; <span style="color: #800080;">$i</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Parent Task $i&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #800080;">$i</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
	<span style="color: #008080; font-weight: bold;">Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #804000;">100</span>;
	<span style="color: #008000;"># Spawn a child Progress bar specify a different Id</span>
	<span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>; <span style="color: #800080;">$x</span> <span style="color: #FF0000;">-le</span> <span style="color: #804000;">10</span>; <span style="color: #800080;">$x</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Child Task $x&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span> <span style="color: pink;">*</span> <span style="color: #804000;">10</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">2</span>;
		<span style="color: #008080; font-weight: bold;">Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #804000;">30</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Spawning multiple progress bars within the Powershell Console" border="0" alt="image thumb1 Using Progress Bars within the Powershell Console" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/image_thumb1.png" width="244" height="159" /></a></p>
<p>To show a practical example, I’ve enhanced a script from a previous post; <a href="http://www.youdidwhatwithtsql.com/check-disk-space-with-powershell-2/" target="_blank">Check disk space with Powershell</a> to use some of the code showcased here (check this for setup instructions). Enjoy!</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p366code34'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p36634"><td class="code" id="p366code34"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Issue warning if % free disk space is less </span>
<span style="color: #800080;">$percentWarning</span> <span style="color: pink;">=</span> <span style="color: #804000;">15</span>;
<span style="color: #008000;"># Get server list</span>
<span style="color: #800080;">$servers</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\serverlist.txt&quot;</span>;
<span style="color: #800080;">$datetime</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span> <span style="color: #008080; font-style: italic;">-Format</span> <span style="color: #800000;">&quot;yyyyMMddHHmmss&quot;</span>;
&nbsp;
<span style="color: #008000;"># Add headers to log file</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\server disks $datetime.txt&quot;</span> <span style="color: #800000;">&quot;server,deviceID,size,freespace,percentFree&quot;</span>;
<span style="color: #008000;"># How many servers</span>
<span style="color: #800080;">$server_count</span> <span style="color: pink;">=</span> <span style="color: #800080;">$servers</span>.Length;
<span style="color: #008000;"># processed server count</span>
<span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
&nbsp;
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$server</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$servers</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$server_progress</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Ceiling<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">/</span> <span style="color: #800080;">$server_count</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #008000;"># Parent progress bar</span>
	<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Checking $server&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #800080;">$server_progress</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing servers - $server_progress%&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
	<span style="color: #008080; font-weight: bold;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>; <span style="color: #008000;"># Sleeping just for progress bar demo</span>
	<span style="color: #008000;"># Get fixed drive info</span>
	<span style="color: #800080;">$disks</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$server</span> <span style="color: #008080; font-style: italic;">-Class</span> Win32_LogicalDisk <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;DriveType = 3&quot;</span>;
&nbsp;
 	<span style="color: #008000;"># How many disks are there?</span>
	<span style="color: #800080;">$disk_count</span> <span style="color: pink;">=</span> <span style="color: #800080;">$disks</span>.Length;
&nbsp;
 	<span style="color: #800080;">$x</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$disk</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$disks</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$disk_progress</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Ceiling<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span> <span style="color: pink;">/</span> <span style="color: #800080;">$disk_count</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #800080;">$disk_name</span> <span style="color: pink;">=</span> <span style="color: #800080;">$disk</span>.Name;
		<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Checking disk $disk_name&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #800080;">$disk_progress</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing server disks - $disk_progress%&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">2</span>;
		<span style="color: #008080; font-weight: bold;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #800080;">$deviceID</span> <span style="color: pink;">=</span> <span style="color: #800080;">$disk</span>.DeviceID;
		<span style="color: #000000;">&#91;</span><span style="color: #008080;">float</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$size</span> <span style="color: pink;">=</span> <span style="color: #800080;">$disk</span>.Size;
		<span style="color: #000000;">&#91;</span><span style="color: #008080;">float</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$freespace</span> <span style="color: pink;">=</span> <span style="color: #800080;">$disk</span>.FreeSpace;
&nbsp;
		<span style="color: #800080;">$percentFree</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Round<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$freespace</span> <span style="color: pink;">/</span> <span style="color: #800080;">$size</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: pink;">,</span> <span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #800080;">$sizeGB</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Round<span style="color: #000000;">&#40;</span><span style="color: #800080;">$size</span> <span style="color: pink;">/</span> <span style="color: #804000;">1073741824</span><span style="color: pink;">,</span> <span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #800080;">$freeSpaceGB</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Round<span style="color: #000000;">&#40;</span><span style="color: #800080;">$freespace</span> <span style="color: pink;">/</span> <span style="color: #804000;">1073741824</span><span style="color: pink;">,</span> <span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #800080;">$colour</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Green&quot;</span>;
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$percentFree</span><span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$percentWarning</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$colour</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Red&quot;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> <span style="color: #800080;">$colour</span> <span style="color: #800000;">&quot;$server $deviceID percentage free space = $percentFree&quot;</span>;
		<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\server disks $datetime.txt&quot;</span> <span style="color: #800000;">&quot;$server,$deviceID,$sizeGB,$freeSpaceGB,$percentFree&quot;</span>;
		<span style="color: #800080;">$x</span><span style="color: pink;">++</span>;
	<span style="color: #000000;">&#125;</span>
	<span style="color: #008000;"># Finish off the progress bar</span>
	<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Finshed checking disks for this server&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #804000;">100</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Done - 100%&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">2</span>;
	<span style="color: #008080; font-weight: bold;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>; <span style="color: #008000;"># Just so we see!</span>
	<span style="color: #800080;">$i</span><span style="color: pink;">++</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Checked all servers&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #804000;">100</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Done - 100%&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
<span style="color: #008080; font-weight: bold;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p>&#160;<a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/check_disk_powershell_progress_bars.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Powershell script checking disk space with progress bars" border="0" alt="check disk powershell progress bars thumb Using Progress Bars within the Powershell Console" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/check_disk_powershell_progress_bars_thumb.png" width="244" height="134" /></a></p>
<p><map name='google_ad_map_366_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/366?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_366_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=366&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fusing-progress-bars-within-the-powershell-console%2F366' title="Using Progress Bars within the Powershell Console" alt=" Using Progress Bars within the Powershell Console" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366">Using Progress Bars within the Powershell Console</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

