<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://powershell.com/cs/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results for 'app:weblogs' matching tags 'Windows PowerShell', '2012 Scripting Games', and 'scripting techniques'</title><link>http://powershell.com/cs/search/SearchResults.aspx?q=app:weblogs&amp;tag=Windows+PowerShell,2012+Scripting+Games,scripting+techniques&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tags 'Windows PowerShell', '2012 Scripting Games', and 'scripting techniques'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 (Build: 30929.2835)</generator><item><title>Scripting Games Wrap Up Part 2: Neatness Counts and Other Stuff</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/04/15/scripting-games-wrap-up-part-2-neatness-counts-and-other-stuff.aspx</link><pubDate>Sun, 15 Apr 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:15971</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: Microsoft Scripting Guy, Ed Wilson, talks about the importance of formatting your Windows PowerShell code, returning objects, and other stuff.&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. It is Sunday in Charlotte, North Carolina. I have been busy grading scripts since early this morning. The last event, Event 10, appeared on April 13, but there is still one more week during which time contestants are turning in their final scripts. This is the time when the leaderboard will begin to dramatically shift. So I still cannot talk about issues from the last five events, but I can talk about some of the earlier events.&lt;/p&gt;
&lt;h1&gt;Neatness counts&lt;/h1&gt;
&lt;p&gt;I am not talking about being really crazy and making sure that everything lines up perfectly. I had a professor in college who used to take a ruler to the green bar paper we had to turn in, and he subtracted 1 point for each space that was not perfectly aligned. Regardless of how the code ran, it had to look perfect&amp;mdash;in fact; it had to print out perfectly. I am NOT talking about that at all.&lt;/p&gt;
&lt;p&gt;But, keep in mind that a fundamental concept is that you should be able to read and understand your script. If you can do that, you reduce the amount of troubleshooting by at least 50% (and no, I have not actually measured that&amp;mdash;it is more of a guess). It is in my &lt;a href="http://www.amazon.com/Windows-PowerShell-2-0-Best-Practices/dp/0735626464/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1255959455&amp;amp;sr=8-1" target="_blank"&gt;Windows PowerShell Best Practices&lt;/a&gt; book in the chapter about troubleshooting scripts.&lt;/p&gt;
&lt;p&gt;So, what am I talking about when I talk about neatness? Here are a few quick guidelines:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Avoid making someone scroll. Keep your code to the width of your screen, so that it does not flow off of the screen. When code is hidden from view, it is easy to make mistakes.&lt;/li&gt;
&lt;li&gt;Avoid using line continuation marks. The grave accent ( &lt;b&gt;`&lt;/b&gt; &amp;hellip;I call it a backtick) is line continuation. It makes code hard to read, and is especially error prone. I have had many scripts that ran perfectly on my computer. Then when I posted them to the Hey, Scripting Guy! Blog, they no longer worked because it picked up an extra space or something. The grave accent is picky, and it is best to avoid it if possible.&lt;/li&gt;
&lt;li&gt;Break your code at the pipe ( &lt;b&gt;|&lt;/b&gt; ) character. Leave your pipe characters on the right, and thus avoid line continuation.&lt;/li&gt;
&lt;li&gt;Indent subordinate clauses. Let&amp;rsquo;s say that you have an &lt;i&gt;IF &lt;/i&gt;statement and your condition on one line. Now you have an &lt;i&gt;ELSE &lt;/i&gt;clause. Intent the &lt;i&gt;ELSE &lt;/i&gt;clause. How much? I generally use two spaces. The reason is that it is a visible indicator of an indent (it is easier to spot than one space), but it does not use as much space as a tab. Remember Rule 1: Keep your code narrow and avoid scrolling. If you use eight spaces for a tab, and you press tab tab tab, you just ate up 24 spaces, and you do not have much room for code after that. So I keep my indents to two spaces.&lt;/li&gt;
&lt;li&gt;Avoid using the &lt;b&gt;Tab&lt;/b&gt; key for your tabs. This is because an actual &lt;b&gt;Tab&lt;/b&gt; key (ASCII 9) is different than a space (ASCII 32). Some editors allow you to substitute spaces for the &lt;b&gt;Tab&lt;/b&gt; key, and they even allow you to specify how many spaces to insert when you do hit the &lt;b&gt;Tab&lt;/b&gt; key. This is useful, because some mediums do not correctly interpret a tab when it shows up in code. For this reason, I avoid using the actual &lt;b&gt;Tab&lt;/b&gt; key, and I instead use spaces.&lt;/li&gt;
&lt;li&gt;Consider where you put the braces (&lt;b&gt;{ }&lt;/b&gt;). This is really a personal preference. I played with putting the braces on their own lines, and lining up the opening and the closing brace. I fact, I still often do that. But at times, when the script is short, I will open the brace on the beginning line, and close it at the end of the last line; and therefore, the braces are not on their own individual lines.&lt;/li&gt;
&lt;li&gt;Avoid in-line comments. Instead put the comments on the line ahead of the line it comments. This is because when code is moved around, edited, or whatever, an in-line comment can break the code that follows. In addition, in-line comments are not as noticeable as comments on their own individual lines.&lt;/li&gt;
&lt;/ol&gt;
&lt;h1&gt;Return an object&amp;hellip;please&lt;/h1&gt;
&lt;p&gt;It is best if you return an object. This is because anyone who uses your code can pipe the results to other cmdlets, such as &lt;b&gt;Where-Object&lt;/b&gt;, &lt;b&gt;Sort-Object&lt;/b&gt;, or even &lt;b&gt;Format-Table&lt;/b&gt; or &lt;b&gt;Out-GridView&lt;/b&gt;. But they are not required to do so. If you spend a lot of time in your script creating your own custom formatting, the person who uses your code cannot process it further. When you send something to &lt;b&gt;Format-Table&lt;/b&gt;, &lt;b&gt;Format-List&lt;/b&gt;, or &lt;b&gt;Format-Wide&lt;/b&gt;, you are done. No further processing can be done because you no longer have the original object&amp;mdash;you have a series of formatting objects that behave completely differently than the original object. When you return an object, you permit others to use your code in the way that they want to use it. It makes the eco-system better.&lt;/p&gt;
&lt;h1&gt;Meet the requirements&lt;/h1&gt;
&lt;p&gt;If my boss asks me to write a script that does x, y, and z, and I spend two days creating a really cool script that does a, b, and c, my boss is not going to be too happy. Because although he now has a script that does a, b, and c, he still needs a script to do x, y, and z. Part of developing solutions involves first analyzing the requirements, determining the inputs and the outputs from the script, examining use case scenarios, and finally, beginning to write the code. In fact, in many projects, the actual coding time is far less than the other tasks put together.&lt;/p&gt;
&lt;p&gt;Well, that is about all I have time for today. I have a lot of scripts to grade. Take care, and I will see you tomorrow when I will have expert solutions to Beginner and Advanced Events 1.&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3491074" width="1" height="1" alt="" /&gt;</description></item><item><title>Simplify Your PowerShell Code by Using a Compound Where Clause</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/04/14/simplify-your-powershell-code-by-using-a-compound-where-clause.aspx</link><pubDate>Sat, 14 Apr 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:15968</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: Microsoft Scripting Guy, Ed Wilson, talks about a couple of issues he saw while grading scripts for the 2012 Windows PowerShell Scripting Games.&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I are in &lt;a href="http://www.sqlsaturday.com/111/eventhome.aspx" target="_blank"&gt;Atlanta for SQL Saturday #111&lt;/a&gt;. I will be talking about Windows PowerShell and SQL best practices. One of the great things about the Scripting Games is seeing what other people are doing. When you are the Microsoft Scripting Guy, it is good to see where people still have some confusion, and then use that information to generate ideas for blog posts. I am not alone in this endeavor. I saw a tweet the other day that said the person was generating a huge list of subjects for blog posts as a result of the 2012 Scripting Games. Today I want to look at a few things that caused me to repeat myself over and over again as I was grading scripts.&lt;/p&gt;
&lt;h1&gt;The mystery of the compound Where clause&lt;/h1&gt;
&lt;p&gt;In Windows PowerShell 3.0, we have simplified the simple &lt;b&gt;Where&lt;/b&gt; clause. I am not going to write about that right now, but it is enough to say that when you have a single condition, it is easier to write in Windows PowerShell&amp;nbsp;3.0 than in Windows PowerShell&amp;nbsp;2.0. But there are many times when you want to use a compound &lt;b&gt;Where&lt;/b&gt; clause. For example, Beginner Event 3 requires you to find running services that also support a &lt;b&gt;Stop&lt;/b&gt; command. I saw lots of scripts where the person obviously did not know about using a compound &lt;b&gt;Where&lt;/b&gt; clause. Here is one such example:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;gsv -computername localhost | where {($_.status -eq &amp;quot;Running&amp;quot;) -and ($_.CanStop -eq $true)}&lt;/p&gt;
&lt;p&gt;Now, there is nothing wrong with this line of code&amp;mdash;in fact, it meets the needs of the scenario, and it works perfectly fine. But the code can be simplified by using a compound &lt;b&gt;Where&lt;/b&gt; clause. Simple and compound do not often go together, but they do here. Here is the way I would write the &lt;b&gt;Where&lt;/b&gt; clause:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;where { $_.status -eq &amp;#39;running&amp;#39; -and $_.canstop }&lt;/p&gt;
&lt;p&gt;Notice, that instead of having two separate groupings, I in fact, have no grouping at all. (The grouping is the parentheses.) The compound &lt;b&gt;Where&lt;/b&gt; clause says where the status of the current object (the &lt;b&gt;$_&lt;/b&gt; represents, the current object in the pipeline) is equal to &lt;b&gt;Running&lt;/b&gt;&lt;i&gt;. &lt;/i&gt;It also says, AND &lt;b&gt;$_.CanStop&lt;/b&gt;. (I will talk about this in a little bit.) &lt;b&gt;Where&lt;/b&gt; is an alias for the &lt;b&gt;Where-Object&lt;/b&gt; cmdlet. The question mark (&lt;b&gt;?&lt;/b&gt;) is also an alias for the &lt;b&gt;Where-Object&lt;/b&gt; cmdlet. But notice that in the script block of the &lt;b&gt;Where-Object&lt;/b&gt; cmdlet, condition one is equal to &lt;b&gt;Running&lt;/b&gt;, AND condition two exists.&lt;/p&gt;
&lt;h1&gt;What is a Boolean value, and what do I do about it?&lt;/h1&gt;
&lt;p&gt;In the get running services that also accept a &lt;b&gt;Stop&lt;/b&gt; command event, there is a property called &lt;b&gt;CanStop&lt;/b&gt;&lt;i&gt; &lt;/i&gt;that exists on the System.ServiceProcess.ServiceController object. This &lt;b&gt;CanStop&lt;/b&gt;&lt;i&gt; &lt;/i&gt;property is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\Users\ed.IAMMRED&amp;gt; Get-Service | Get-Member canstop&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp; TypeName: System.ServiceProcess.ServiceController&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Name&amp;nbsp;&amp;nbsp;&amp;nbsp; MemberType Definition&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;----&amp;nbsp;&amp;nbsp;&amp;nbsp; ---------- ----------&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;CanStop Property&amp;nbsp;&amp;nbsp; System.Boolean CanStop {get;}&lt;/p&gt;
&lt;p&gt;Notice that under the definition, it says that the &lt;b&gt;CanStop&lt;/b&gt;&lt;i&gt; &lt;/i&gt;property is a System.Boolean. Boolean values are On/Off, True/False, -1/0, Enabled/Disabled, Exist/Don&amp;rsquo;t exist, and so on. One of the cool things about Windows PowerShell, is that if something exists, and it is a Boolean value, I only need to say &lt;b&gt;Where&lt;/b&gt; plus &lt;i&gt;ThePropertyOfTheObject. &lt;/i&gt;Let&amp;rsquo;s go back to Beginner Event 3. So, in the long version of the &lt;b&gt;Where&lt;/b&gt; clause, the writer had the following:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;where {($_.status -eq &amp;quot;Running&amp;quot;) -and &lt;b&gt;($_.CanStop -eq $true&lt;/b&gt;)}&lt;/p&gt;
&lt;p&gt;As I mentioned, it works. But it can be shortened by saying &lt;b&gt;&amp;ndash;and $_.CanStop&lt;/b&gt;. Here we are saying, &amp;ldquo;Does the &lt;b&gt;CanStop&lt;/b&gt;&lt;i&gt; &lt;/i&gt;property exist on the object?&amp;rdquo; If it does, this is all we want. Therefore, I can reduce this by saying, &amp;ldquo;Does it exist?&amp;rdquo; This is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;where { $_.status -eq &amp;#39;running&amp;#39; -and&lt;b&gt; $_.canstop&lt;/b&gt; }&lt;/p&gt;
&lt;p&gt;The nice thing is that I can negate the Boolean value. For example, if I want the running services that do NOT accept a &lt;b&gt;Stop&lt;/b&gt; command, I have several choices. I would probably use the &lt;b&gt;Not&lt;/b&gt; operator (&lt;b&gt;!&lt;/b&gt;). This is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;where { $_.status -eq &amp;#39;running&amp;#39; -AND !$_.canstop }&lt;/p&gt;
&lt;p&gt;Most of the time, I will use grouping around the Boolean value to make the code a bit clearer. This is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;where { $_.status -eq &amp;#39;running&amp;#39; -AND !($_.canstop)&lt;/p&gt;
&lt;p&gt;If you do not like the exclamation point (&lt;b&gt;!&lt;/b&gt;) for the &lt;b&gt;Not&lt;/b&gt; operator, you also can use the &lt;b&gt;&amp;ndash;not&lt;/b&gt; operator. This is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;where { $_.status -eq &amp;#39;running&amp;#39; -AND -not $_.canstop }&lt;/p&gt;
&lt;p&gt;Once again, I generally put grouping around the Boolean value to make the command easier to read (at least for me). This is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;where { $_.status -eq &amp;#39;running&amp;#39; -AND -not ($_.canstop) }&lt;/p&gt;
&lt;h1&gt;Not handling the default&lt;/h1&gt;
&lt;p&gt;I also saw lots of scripts that had a hardcoded value for &lt;b&gt;ComputerName&lt;/b&gt;. Some said &lt;i&gt;Server01, &lt;/i&gt;others said &lt;i&gt;NameOfRemoteServer. &lt;/i&gt;I am calling this out because it is a problem. It causes someone to immediately edit the script before they can run it. I wrote a really good blog about this issue that I will refer you to: &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/21/create-and-use-default-values-in-powershell-scripts.aspx" target="_blank"&gt;Create and Use Default Values in PowerShell Scripts.&lt;/a&gt;&lt;i&gt; &lt;/i&gt;&lt;/p&gt;
&lt;p&gt;Well, that is about it for now. There are thousands of scripts that need to be graded in the next few days. So until tomorrow, have a great day.&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3491043" width="1" height="1" alt="" /&gt;</description></item><item><title>2012 Scripting Games Advanced Event 9: Perform an Inventory</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/04/12/2012-scripting-games-advanced-event-9-perform-an-inventory.aspx</link><pubDate>Thu, 12 Apr 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:15933</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&amp;nbsp;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8203.hsg_2D00_2_2D00_4_2D00_12_2D00_1.png"&gt;&lt;img title="2012 Scripting Games badge" alt="2012 Scripting Games badge" src="http://blogs.technet.com/resized-image.ashx/__size/150x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8203.hsg_2D00_2_2D00_4_2D00_12_2D00_1.png" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: In Advanced Event 9, you are required to perform a hardware inventory and write the information to an XML file.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="TableNum-Title"&gt;&lt;strong&gt;About this event&lt;/strong&gt;&lt;/p&gt;
&lt;table cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Division&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Advanced&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Date of Event&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;4/12/2012 12:01 AM&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Due Date&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;4/19/2012 12:01 AM&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2&gt;Event scenario&lt;/h2&gt;
&lt;p&gt;You are a network manager at a medium-sized organization, and it is time to perform an inventory of computers on the network. You need to collect the following information: computer name, domain name, computer manufacturer, computer model, number of processors, number of cores, speed of processors, processor ID, MAC address of the primary network interface, operating system version (including service pack level), and the amount of physical memory that is installed (displayed in the most logical units). Your output should be stored in an XML-formatted file in the &lt;i&gt;Documents &lt;/i&gt;special folder. The file name should be the Computername (including domain name) and the date in year, month, day format.&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Design points&lt;/h2&gt;
&lt;p&gt;There are several requirements for this scenario:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Obtaining the information.&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Storing the output in XML format.&lt;/li&gt;
&lt;li&gt;Creating the file in a specific location&lt;/li&gt;
&lt;li&gt;Creating the file name in the specific fashion, for example:&lt;br /&gt;&lt;i&gt;Mycomputer.mydomain.20120412.XML&lt;/i&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Because the script will run locally on the computer, it does not need the ability to run against remote computers.&lt;/li&gt;
&lt;li&gt;Extra points for adding error handling for missing properties&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;2012 Scripting Games links&lt;/h2&gt;
&lt;p style="padding-left:30px;"&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/04/the-2012-windows-powershell-scripting-games-all-links-on-one-page.aspx" target="_blank"&gt;&lt;strong&gt;2012 Scripting Games: All Links on One Page&lt;/strong&gt;&lt;/a&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;I invite you to follow me on&amp;nbsp;&lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at&amp;nbsp;&lt;a href="mailto:scripter@microsoft.com"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the&amp;nbsp;&lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. Good luck as you compete in this year&amp;rsquo;s Scripting Games. We wish you well.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3487515" width="1" height="1" alt="" /&gt;</description></item><item><title>2012 Scripting Games Advanced Event 6: Compute Uptime for Multiple Servers</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/04/09/2012-scripting-games-advanced-event-6-compute-uptime-for-multiple-servers.aspx</link><pubDate>Mon, 09 Apr 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:15867</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&amp;nbsp;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8203.hsg_2D00_2_2D00_4_2D00_12_2D00_1.png"&gt;&lt;img title="2012 Scripting Games badge" alt="2012 Scripting Games badge" src="http://blogs.technet.com/resized-image.ashx/__size/150x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8203.hsg_2D00_2_2D00_4_2D00_12_2D00_1.png" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: In Advanced Event 6, you are required to compute the uptime for multiple servers.&lt;/p&gt;
&lt;p class="TableNum-Title"&gt;&lt;strong&gt;About this event&lt;/strong&gt;&lt;/p&gt;
&lt;table cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Division&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Advanced&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Date of Event&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;4/9/2012 12:01 AM&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Due Date&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;4/16/2012 12:01 AM&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2&gt;Event scenario&lt;/h2&gt;
&lt;p&gt;You are the server lead for a medium-sized enterprise, and your boss has tasked you with computing the uptime of all servers on the network on a daily basis. This information will be stored in a Microsoft Excel spreadsheet for ease of analysis. Because of the large number of servers, the script will take some time to run; therefore, your boss has decided to compute all uptime as of 8:00 AM local time. If a server reboots after 8:00 AM, but before the report runs, the uptime is zero, and it will be factored into the report for the next day. Create a new report file each day for all the servers on the list. An acceptable output is shown in the image that follows.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6177.adv_2D00_6_2D00_2012.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6177.adv_2D00_6_2D00_2012.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Design points&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Your script should be capable of running more than once per day. If it does, it should not generate an error.&lt;/li&gt;
&lt;li&gt;Create the CSV file in the logged-on users &lt;i&gt;Documents&lt;/i&gt; special folder.&lt;/li&gt;
&lt;li&gt;The file name should use the year, month, and day that the report runs, with the addition of _Uptime to the file. It will appear like the following: 20120409_Uptime.csv&lt;/li&gt;
&lt;li&gt;The column headers should appear on the first row only. The headings should be &lt;i&gt;ComputerName, Days, Hours, Minutes, Seconds,&lt;/i&gt; and &lt;i&gt;Date&lt;/i&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;2012 Scripting Games links&lt;/h2&gt;
&lt;p style="padding-left:30px;"&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/04/the-2012-windows-powershell-scripting-games-all-links-on-one-page.aspx" target="_blank"&gt;&lt;strong&gt;2012 Scripting Games: All Links on One Page&lt;/strong&gt;&lt;/a&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;I invite you to follow me on&amp;nbsp;&lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at&amp;nbsp;&lt;a href="mailto:scripter@microsoft.com"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the&amp;nbsp;&lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. Good luck as you compete in this year&amp;rsquo;s Scripting Games. We wish you well.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3487131" width="1" height="1" alt="" /&gt;</description></item><item><title>2012 Scripting Games Beginner Event 6: Compute Uptime for Local Computer</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/04/09/2012-scripting-games-beginner-event-6-compute-uptime-for-local-computer.aspx</link><pubDate>Mon, 09 Apr 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:15866</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&amp;nbsp;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8203.hsg_2D00_2_2D00_4_2D00_12_2D00_1.png"&gt;&lt;img title="2012 Scripting Games badge" alt="2012 Scripting Games badge" src="http://blogs.technet.com/resized-image.ashx/__size/150x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8203.hsg_2D00_2_2D00_4_2D00_12_2D00_1.png" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: In Beginner Event 6, you are required to compute the uptime for the local computer.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;About this event&lt;/strong&gt;&lt;/p&gt;
&lt;table cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Division&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Beginner&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Date of Event&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;4/9/2012 12:01 AM&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Due Date&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;4/16/2012 12:01 AM&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2&gt;Event scenario&lt;/h2&gt;
&lt;p&gt;You are the main network administrator for a small company. As such, your duties consist of a variety of functions, including assisting the Help Desk when they get behind. Recently, your manager has become concerned about server uptime. He wants you to write a script that will display how long a server has been &amp;ldquo;up.&amp;rdquo; He said that he is not concerned with anything fancy&amp;mdash;he just wants a general idea. The only real guidance he provided for the task is, &amp;ldquo;Use WMI. There is a class called Win32_OperatingSystem that should do the trick for you. Tell me the server name, and how many days, hours, and minutes the server has been up.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;The output that is shown in the image that follows meets the boss&amp;rsquo;s requirements.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2477.beg_2D00_6_2D00_2012.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2477.beg_2D00_6_2D00_2012.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Design points&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Your solution only needs to run locally.&lt;/li&gt;
&lt;li&gt;Your script should be easy to read and easy to understand.&lt;/li&gt;
&lt;li&gt;Your script is only concerned with the uptime for the local server, calculated with the current time. Do not worry about time zones or about comparing up times with different computers.&lt;/li&gt;
&lt;li&gt;Pay attention to the formatting of the output and the script itself.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;2012 Scripting Games links&lt;/h2&gt;
&lt;p style="padding-left:30px;"&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/04/the-2012-windows-powershell-scripting-games-all-links-on-one-page.aspx" target="_blank"&gt;&lt;strong&gt;2012 Scripting Games: All Links on One Page&lt;/strong&gt;&lt;/a&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;I invite you to follow me on&amp;nbsp;&lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at&amp;nbsp;&lt;a href="mailto:scripter@microsoft.com"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the&amp;nbsp;&lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. Good luck as you compete in this year&amp;rsquo;s Scripting Games. We wish you well.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3487130" width="1" height="1" alt="" /&gt;</description></item><item><title>Six Observations about 2012 Scripting Games Early Entries</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/04/07/six-observations-about-2012-scripting-games-early-entries.aspx</link><pubDate>Sat, 07 Apr 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:15858</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: Microsoft Scripting Guy, Ed Wilson, discusses some of the early entries to the 2012 Windows PowerShell Scripting Games.&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. One of the things that is always a challenge in creating the Scripting Games is trying to come up with the right balance between challenge, practicality, and appropriateness. In some respects, it is like attempting to walk a six inch I-beam 100 stories up in the air. It looks more precarious that it really is. But like one sage told Harry Potter, &amp;ldquo;You best keep your wits about you.&amp;rdquo; And so it is with the Scripting Games&amp;mdash;you best keep your wits about you.&lt;/p&gt;
&lt;h1&gt;Complexity is not always required&lt;/h1&gt;
&lt;p&gt;This year, I tried. I really did try to reduce complexity. One of my goals for this year&amp;rsquo;s Scripting Games is to force you into the documentation (think about using the &lt;b&gt;Get-Help&lt;/b&gt; cmdlet on a regular basis) so that you will discover some hidden gem hidden amongst the thousands of parameters that exist for the hundreds of Windows PowerShell&amp;nbsp;cmdlets. Obviously, I was not entirely successful.&lt;/p&gt;
&lt;p&gt;The point is this: In Windows PowerShell, lots of tasks are ridiculously simple when you have figured out the secret. Of course, when a parameter exists and it is documented in &lt;b&gt;Get-Help&lt;/b&gt;, I am not sure that it really qualifies as a secret&amp;mdash;but hey, who am I to argue?&lt;/p&gt;
&lt;h1&gt;Some things really are easy&lt;/h1&gt;
&lt;p&gt;It has become nearly a clich&amp;eacute; since I first wrote that there are three important cmdlets in Windows PowerShell. These three cmdlets are &lt;b&gt;Get-Help&lt;/b&gt;, &lt;b&gt;Get-Command&lt;/b&gt;, and &lt;b&gt;Get-Member&lt;/b&gt;. It was really important in Windows PowerShell&amp;nbsp;1.0 when I first set this down. For beginners, in Windows PowerShell&amp;nbsp;2.0 (and especially in Windows PowerShell&amp;nbsp;3.0), I do not think that &lt;b&gt;Get-Member&lt;/b&gt; is quite as important as the other two discovery&lt;i&gt; &lt;/i&gt;cmdlets. This is because each new version adds new cmdlets and additional parameters that bring interesting members to the foreground, therefore make discovering obscure members less important. Even with Windows PowerShell&amp;nbsp;3.0, using &lt;b&gt;Get-Member&lt;/b&gt; is important for the more advanced scripter. But each new version makes it easier to perform common tasks, so digging into the object model becomes less of a daily task. I think that these days, I use &lt;b&gt;Select-Object&lt;/b&gt; and &lt;b&gt;Measure-Object&lt;/b&gt; more often than I use &lt;b&gt;Get-Member&lt;/b&gt;.&lt;/p&gt;
&lt;h1&gt;Scripting is not required&lt;/h1&gt;
&lt;p&gt;I know they are called the Scripting Games, but this is more a legacy artifact of the VBScript world that spawned the first Scripting Guys and consequently the first Scripting Games. Nearly all of the Beginner events in this year&amp;rsquo;s Scripting Games (and many of the Advanced events) can be completed with a one-liner. To some, these famously (or infamously) powerful commands epitomize the elegance of the Windows PowerShell language. You can save a one-liner in a .ps1 file and voila, you have a script. Of course, you can also save your one-liner in a text file if you wish. In fact, I actually do both. I have lots of .txt files that contain various one-line Windows PowerShell commands that I spent a decent amount of time creating; and therefore, I wanted a way to save the product of my brilliance.&lt;/p&gt;
&lt;p&gt;Keep in mind that at its most basic element, a Windows PowerShell script is simply a collection of one or more Windows PowerShell commands. If I can type it in the Windows PowerShell console, I can place it in a .ps1 file, and I have a Windows PowerShell script. This is one of the great things about Windows PowerShell&amp;mdash;it is a shell. It is also a scripting language. It is also a shell. It is also a scripting language. It is like a chocolate peanut butter cup&amp;mdash;it is chocolate, but it is also peanut butter. When I teach a Windows PowerShell class, I generally spend the first two days working exclusively inside the Windows PowerShell console. But, I make it really clear to the students that everything we are doing applies to Windows PowerShell scripting. I love a tweet Jeffrey Hicks made when he showed dumping the commands from command history to a .ps1 file and automatically creating a script from his Windows PowerShell console command history. This is the perfect example that working interactively at the Windows PowerShell console is, in a real sense, scripting.&lt;/p&gt;
&lt;p&gt;Now, let&amp;rsquo;s get back to my Windows PowerShell classes. Even after I finally introduce the Windows PowerShell ISE, I still end up doing many examples directly in the Windows PowerShell console. One reason is because the Windows PowerShell console has a built in transcription tool, and the Windows PowerShell ISE does not. Being able to hand out a transcript of everything that I typed in a class makes a great teaching tool. It also prevents writer&amp;rsquo;s cramp on behalf of the students.&lt;/p&gt;
&lt;p&gt;Sometimes it seems that students are disappointed they do not do more traditional scripting in my Windows PowerShell classes. But the simple fact is that in the Windows PowerShell era, quite often scripting is not required. Many traditional tasks that required lengthy and complex scripts in the VBScript world, are simple one-liners in Windows PowerShell. Like I said, if you miss scripting, go ahead and save your one-liners as .ps1 files.&lt;/p&gt;
&lt;h1&gt;A great work ethic does not require lengthy scripts&lt;/h1&gt;
&lt;p&gt;In Windows PowerShell, often you do not need to write a big, long, complex script. In fact, I would say that on many occasions, this is true. It has quickly become the rule, rather than the exception. I can also say from experience, at times I am still too quick to fire up the Windows PowerShell ISE. After all these years of exclusively writing Windows PowerShell on nearly a daily basis, I am still surprised at how easy things can be accomplished. So what am I really saying? In Windows PowerShell, often more work goes into producing a one-liner than in creating a 15-line script. When I am making presentations to a Windows PowerShell User Group, I am often asked questions, and I will respond by firing up the Windows PowerShell ISE and dashing off a quick script to illustrate the technique I am trying to explain. At times, someone in the group will ask why I did not do XYZ. Often the answer is that I did not think of doing XYZ. Windows PowerShell is so powerful, that there is often more than one way of accomplishing the same task.&lt;/p&gt;
&lt;h1&gt;In the end, it is about getting work accomplished&lt;/h1&gt;
&lt;p&gt;In the real world, and in the real world of the 2012 Scripting Games, the end game is to accomplish work. As long as a script or a command accomplishes the task at hand, it works. Jeffrey Snover once remarked that no one learns Windows PowerShell simply to learn Windows PowerShell. Rather, they use Windows PowerShell to solve a specific problem. This is one thing I try to reflect in the Scripting Games&amp;mdash;real-world problems seeking real-world solutions. But I do disagree with Jeffrey in one important respect. I know lots of people who have absolutely fallen in love with Windows PowerShell simply because Windows PowerShell is fun, powerful, and a great tool. Many people, including myself, find ourselves playing with Windows PowerShell and exploring the intricacies of Windows PowerShell because of this fun factor. The person who wrote Space Invaders in Windows PowerShell did not do so because his boss asked him for a Windows PowerShell port of Space Invaders. Rather he did it because Windows PowerShell is so fun to play with, and it is great that it has the ability to create fun games like Space Invaders.&lt;/p&gt;
&lt;h1&gt;Great solutions to regular problems&lt;/h1&gt;
&lt;p&gt;One reason I love the Scripting Games is that I also view myself as a learner. Whereas I write solutions to each of the tasks, I am often surprised at the sheer elegance of some of the solutions that come in from the participants. In many cases, I look at code and wonder, &amp;ldquo;Does that work?&amp;rdquo; I stare in amazement when it really does. This is one of the most exciting things about Windows PowerShell&amp;mdash;there are many many many ways of accomplishing the same tasks. The great thing about the 2012 Scripting Games is that you work out solutions to real-world problems. I have had numerous emails from participants in prior Scripting Games where they thanked me for the games and stated that they had already had occasion to use a script at work that they wrote for the games. Cool. Learning that is fun, and useful&amp;mdash;a great concept.&lt;/p&gt;
&lt;p&gt;Join me tomorrow when I will continue to talk about some of the concerns I saw while grading scripts.&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3489878" width="1" height="1" alt="" /&gt;</description></item><item><title>Use PowerShell to Troubleshoot and Repair WMI Errors</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/03/29/use-powershell-to-troubleshoot-and-repair-wmi-errors.aspx</link><pubDate>Thu, 29 Mar 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:15669</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: Learn how to use Windows PowerShell to troubleshoot and repair WMI errors.&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. Tomorrow is the &lt;a href="http://techstravaganza.wordpress.com/" target="_blank"&gt;New York City TechStravaganza&lt;/a&gt;. The Scripting Wife and I will be there, and we are looking forward to hanging out with Microsoft PowerShell MVPs such as Tome, Brandon, and Aleksandar. Anyway, it was late when we got to the hotel, and I was looking forward to reading a story collection by Raymond Chandler and calling it a day. I had just started on the third story in the collection, when above the constant hum of Times Square, I heard the Scripting Wife.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Why do I always get these errors,&amp;rdquo; she exclaimed with obvious annoyance.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Probably because you are doing something wrong,&amp;rdquo; I said not really helping the situation.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;No. In the event log. Here take a look,&amp;rdquo; she said turning her laptop to me.&lt;/p&gt;
&lt;p&gt;The error to which she referred is shown here.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/0638.HSG_2D00_3_2D00_29_2D00_12_2D00_01.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/0638.HSG_2D00_3_2D00_29_2D00_12_2D00_01.png" alt="Image of error message" title="Image of error message" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;How do you know you are always getting this error,&amp;rdquo; I asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Because I always see it every time I open the Application log on my computer,&amp;rdquo; she replied.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Well, let&amp;rsquo;s see how many times you really are getting this error,&amp;rdquo; I suggested, &amp;ldquo;Open the Windows PowerShell console, and let&amp;rsquo;s use the &lt;b&gt;Get-WinEvent&lt;/b&gt; cmdlet to retrieve every Event 10 from Windows Management Instrumentation.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;OK. But you are going to have to help me a bit,&amp;rdquo; she said.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;But of course. We will use a filter hash table so that we only retrieve the events from Windows Management Instrumentation that are Event 10 in the Application log. The first thing is to type the &lt;b&gt;Get-WinEvent&lt;/b&gt; cmdlet, and call the &lt;i&gt;FilterHashTable &lt;/i&gt;parameter. But do not press ENTER because we will build the command.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;The Scripting Wife thought for a bit, and then typed the following (she used tab expansion to complete the &lt;i&gt;FilterHashTable &lt;/i&gt;parameter name).&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-WinEvent -FilterHashtable&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Now we need to create the hash table for the filter. Remember that a hash table begins with the &amp;lsquo;at&amp;rsquo; sign (&lt;b&gt;@&lt;/b&gt;). Then it is the key equals the value. Each pair gets a semicolon separator,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;OK,&amp;rdquo; she said as she typed the &amp;ldquo;at&amp;rdquo; sign, and opened a pair of curly braces, &amp;ldquo;Now what?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;The first pair is &lt;b&gt;LogName&lt;/b&gt;&lt;i&gt; &lt;/i&gt;equals &lt;b&gt;Application&lt;/b&gt;&lt;i&gt;.&lt;/i&gt; Put the word &amp;lsquo;application&amp;rsquo; in quotation marks,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;The following is what the Scripting wife typed.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;@{logname=&amp;#39;application&amp;#39;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Now the next pair is &lt;b&gt;ID&lt;/b&gt;&lt;i&gt; &lt;/i&gt;equals &lt;b&gt;10&lt;/b&gt;. There is no need for quotation marks around the number ten. Remember, that each key value pair receives the semicolon separator.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;The Scripting Wife was all business. The she typed the following.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;;id=10;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;The good thing is that the &lt;i&gt;ProviderName &lt;/i&gt;will accept a wild card. So all you need to do is use the asterisk with the letters wmi. Make sure you also put them in quotation marks and close the curly bracket,&amp;rdquo; I said. &lt;br /&gt; Here is what she typed.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;providername=&amp;#39;*wmi*&amp;#39;}&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Great. You are almost done. Pipe the whole thing to the &lt;b&gt;Measure-Object&lt;/b&gt; cmdlet. You can use the alias &lt;b&gt;measure&lt;/b&gt; for that cmdlet,&amp;rdquo; I instructed.&lt;/p&gt;
&lt;p&gt;The following is the Scripting Wife&amp;rsquo;s completed command.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-WinEvent -FilterHashtable @{logname=&amp;#39;application&amp;#39;;id=10;providername=&amp;#39;*wmi*&amp;#39;}&amp;nbsp; | measure&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Cool, it works,&amp;rdquo; the Scripting Wife exclaimed, &amp;ldquo;Here take a look.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;As she turned her laptop monitor so I could see the following output in her Windows PowerShell console.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/3324.HSG_2D00_3_2D00_29_2D00_12_2D00_02.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/3324.HSG_2D00_3_2D00_29_2D00_12_2D00_02.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;How would I know how to do this if you were not around?&amp;rdquo; she asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;You could use the &lt;b&gt;Get-EventLog&lt;/b&gt; cmdlet to do this, but you would need to pipe the results to the &lt;b&gt;Where-Object&lt;/b&gt; cmdlet to help with the filtering. This command is more efficient. Besides, I wrote a great Hey, Scripting Guy! blog last year about using the &lt;i&gt;FilterHashTable &lt;/i&gt;parameter. It is titled &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/03/08/how-to-improve-the-performance-of-a-powershell-event-log-query.aspx" target="_blank"&gt;How to Improve the Performance of a PowerShell Event Log Query&lt;/a&gt;,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;So what does all this mean anyway?&amp;rdquo; she asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;What does what mean?&amp;rdquo; I asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;What is Event 10 from Windows Management Instrumentation, and why is it littering my Application log with meaningless events?&amp;rdquo; she asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Actually it is not meaningless. It means that some permanent event does not have permission to run. That is what the error code 0x80041003 means. Why it does not have permission is another issue. Let me check with Bing to find out,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;Within less than a minute, I had retrieved &lt;a href="http://support.microsoft.com/default.aspx?scid=kb;en-US;2545227" target="_blank"&gt;a support article describing the situation&lt;/a&gt;. Interestingly enough, this issue also has a Microsoft Fix It solution attached to it. &amp;ldquo;Cool.&amp;rdquo; I thought. Then I thought this would be a great opportunity to talk to the Scripting Wife about permanent event consumers.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Do you remember when Trevor wrote &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/12/08/use-a-powershell-module-to-work-with-wmi-permanent-events.aspx" target="_blank"&gt;Use a PowerShell Module to Work with WMI Permanent Events&lt;/a&gt;?&amp;rdquo; I asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Sure. I thought it was great,&amp;rdquo; she replied.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Well, before that blog, I talked about working with permanent events and I discussed the technology that was behind them in &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/12/07/use-powershell-to-monitor-and-respond-to-events-on-your-server.aspx" target="_blank"&gt;Use PowerShell to Monitor and Respond to Events on Your Server&lt;/a&gt;,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Uh. Sure, if you say so. I don&amp;rsquo;t remember that far back,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;I started to say something like she remembered Trevor&amp;rsquo;s module, but then I decided to keep my mouth shut for a change. Then I changed my mind.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Well, this is your lucky day. Because we are going to do a refresher course,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Oh, come on. It is late, and tomorrow is the TechStravaganza,&amp;rdquo; she whined.&lt;/p&gt;
&lt;p&gt;The first thing I did was create a simple script that I called Get-BVTFilterandConsumer.ps1. This script is shown here. (Note: The second line uses the back tick character for line continuation. &lt;b&gt;GWMI&lt;/b&gt; is an alias for the &lt;b&gt;Get-WmiObject&lt;/b&gt; cmdlet. Remember, this is a quick script, and it is late at night.)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Get-BVTFilterandConsumer.ps1&lt;/b&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;gwmi __EventFilter -Namespace root\subscription -Filter &amp;quot;name = &amp;#39;bvtfilter&amp;#39;&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;gwmi __FilterToConsumerBinding -Namespace root\subscription `&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;-filter &amp;quot;filter = &amp;#39;__EventFilter.Name=&amp;quot;&amp;quot;bvtFilter&amp;quot;&amp;quot;&amp;#39;&amp;quot;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Now I want you to run the script. It will retrieve instances of the __EventFilter Windows Management Instrumentation class that are named &lt;b&gt;bvtFilter&lt;/b&gt;. It will also retrieve instances of the __FilterToConsumerBinding Windows Management Instrumentation class&amp;mdash;but only instances that are associated with the filter named &lt;b&gt;bvtFilter&lt;/b&gt;. It sounds complicated, but really it is not,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;If you say so. But sometimes I wonder if your idea of complicated and my idea of complicated are the same thing,&amp;rdquo; she said as she ran the script. The output from the script is shown in the image that follows.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5381.HSG_2D00_3_2D00_29_2D00_12_2D00_03.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5381.HSG_2D00_3_2D00_29_2D00_12_2D00_03.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Now what we need to do is to delete both the &lt;b&gt;bvtFilter&lt;/b&gt; and the filter to consumer binding. This part is really easy because all we need to do is to pipe the objects from the previous script to &lt;b&gt;Remove-WmiObject&lt;/b&gt;,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;That is the first thing you have said that makes sense,&amp;rdquo; she said, &amp;ldquo;Shouldn&amp;rsquo;t we back up the computer first?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;We do not need to back up the entire computer, but it does not hurt to back up system state. To do this, use the &lt;b&gt;CheckPoint-Computer&lt;/b&gt; cmdlet,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;The Scripting Wife copied the code from the Get-BVTFilterandConsumer.ps1 script, and at the end of each line she added a pipe character, and sent the objects to the &lt;b&gt;Remove-WMIObject&lt;/b&gt; cmdlet. Then she backed up and went to the top of the script and added the &lt;b&gt;Checkpoint-Computer&lt;/b&gt; command. The resulting script, which she named BackupComputerAndRemoveBVTFilter.ps1, is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;BackupComputerAndRemoveBVTFilter.ps1&lt;/b&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Checkpoint-Computer -Description &amp;quot;prior to fixing event 10 from wmi&amp;quot; `&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;-RestorePointType MODIFY_SETTINGS&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;gwmi __EventFilter -Namespace root\subscription -Filter &amp;quot;name = &amp;#39;bvtfilter&amp;#39;&amp;quot; |&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Remove-WmiObject&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;gwmi __FilterToConsumerBinding -Namespace root\subscription `&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;-filter &amp;quot;filter = &amp;#39;__EventFilter.Name=&amp;quot;&amp;quot;bvtFilter&amp;quot;&amp;quot;&amp;#39;&amp;quot; |&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Remove-WmiObject&lt;/p&gt;
&lt;p&gt;&amp;nbsp;When the Scripting Wife pressed the green triangle to run the script, the dialog box shown here appeared.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/7725.HSG_2D00_3_2D00_29_2D00_12_2D00_04.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/7725.HSG_2D00_3_2D00_29_2D00_12_2D00_04.png" alt="Image of dialog box" title="Image of dialog box" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Other than the dialog box, there was no output from the script.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;So how do I know that it worked?&amp;rdquo; she asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;For one thing, you can run my Get-BVTFilterandConsumer.ps1 script. But with no instances, it will not display any output either. Another thing you can do is to open WbemTest, change your namespace to root\subscription and open the __FilterToConsumerBinding Windows Management Instrumentation class,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;The Scripting Wife typed wbemtest into the Windows PowerShell console, and when the Windows Management Instrumentation Tester opened, she set the namespace to root\subscription and opened the __FilterToConsumerBinding Windows Management Instrumentation class. She then pressed the &lt;b&gt;Instances&lt;/b&gt; button and the following dialog box appeared.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5732.HSG_2D00_3_2D00_29_2D00_12_2D00_05.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5732.HSG_2D00_3_2D00_29_2D00_12_2D00_05.png" alt="Image of dialog box" title="Image of dialog box" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;You might also want to look for instances of the __EventFilter Windows Management Instrumentation class,&amp;rdquo; I suggested.&lt;/p&gt;
&lt;p&gt;The Scripting Wife closed the previous dialog box, and opened the __EventFilter class. Next she looked for instances. The results are shown here.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5008.HSG_2D00_3_2D00_29_2D00_12_2D00_06.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5008.HSG_2D00_3_2D00_29_2D00_12_2D00_06.png" alt="Image of dialog box" title="Image of dialog box" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;OK. That&amp;rsquo;s enough. You are making my head hurt, and I need to catch up with people on Facebook,&amp;rdquo; the Scripting Wife said.&lt;/p&gt;
&lt;p&gt;And with that, I was effectively dismissed. 2012 Scripting Games Prep Week will continue tomorrow when we will have a guest blog post from Boe Prox.&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3489015" width="1" height="1" alt="" /&gt;</description></item><item><title>Use PowerShell to Parse an XML File and Sort the Data</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/03/26/use-powershell-to-parse-an-xml-file-and-sort-the-data.aspx</link><pubDate>Mon, 26 Mar 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:15539</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: In this blog, the Scripting Wife learns how to use Windows PowerShell to parse her books XML files and to find authors and sort titles.&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. Today the registration opens for the 2012 Scripting Games. You can go to the &lt;a href="http://2012sg.poshcode.org/" target="_blank"&gt;2012 Scripting Games site&lt;/a&gt; on PoshCode, click Log On, choose your authentication mechanism, and fill out your user name and email address. Make sure your user name is the name you want to appear on the leaderboard, and on your 2012 Scripting Games certificate. Also, make sure your email address is correct because it is used to notify you about prizes.&lt;/p&gt;
&lt;p&gt;Yesterday, the Scripting Wife asked me to help her with &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/03/25/the-scripting-wife-learns-to-use-powershell-to-work-with-xml.aspx" target="_blank"&gt;exploring an XML file&lt;/a&gt;. Last night, she told me that she wants me to help her look at the XML file she got by exporting her book database so that she can find titles of books and things like that. I think I will call her, and see if she is ready&amp;hellip;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Scripting Wife?&amp;rdquo; I call in my nicest voice.&lt;/p&gt;
&lt;p&gt;All of a sudden I look up, and she is here.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;You don&amp;rsquo;t have to yell. You could simply have called me on my Windows&amp;nbsp;7 phone,&amp;rdquo; she replied.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;I didn&amp;rsquo;t yell&amp;mdash;and besides, I was not sure you had your Windows&amp;nbsp;7 phone with you,&amp;rdquo; I apologized.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;I always have it with me,&amp;rdquo; she said rather curtly. &amp;ldquo;So are you going to show me how to find titles in my XML file?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Yep. Why don&amp;rsquo;t you sit over here next to the computer, and first read your XML file into a variable. Remember to use the [XML] symbol to make sure that you get an XML document,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;The Scripting Wife thought for a minute, and then typed the following command.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;[xml]$books = Get-Content C:\fso\books.xml&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Now, remember that all your book data was in the book node. So why don&amp;rsquo;t you retrieve that, pipe it to the &lt;b&gt;Foreach-Object &lt;/b&gt;cmdlet, and then retrieve the &lt;b&gt;Title&lt;/b&gt;&lt;i&gt; &lt;/i&gt;property,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;The Scripting Wife thought for a few seconds and began to type. Within a minute or so, she had composed the command that is shown here. (The percentage sign (%) is an alias for the &lt;b&gt;Foreach-Object &lt;/b&gt;cmdlet.)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$books.bookinfo.booklist.book | % {$_.mainsection.title}&lt;/p&gt;
&lt;p&gt;The two commands and the output associated with the commands are shown in the image that follows.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2234.HSG_2D00_3_2D00_26_2D00_12_2D00_01.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2234.HSG_2D00_3_2D00_26_2D00_12_2D00_01.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;That is pretty cool, but I want to be able to find out how many books I have based on subject,&amp;rdquo; she asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Well, that is pretty easy. Take your book element, pipe it to the &lt;b&gt;Foreach-Object &lt;/b&gt;cmdlet, then pipe that to the &lt;b&gt;Sort-Object&lt;/b&gt; cmdlet, and choose the &lt;b&gt;DisplayName&lt;/b&gt; property. Then group it by &lt;b&gt;DisplayName&lt;/b&gt;. Use the &lt;i&gt;NoElement &lt;/i&gt;switched parameter to keep from displaying a bunch of extra information,&amp;rdquo; I said.&lt;b&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;She thought for a second, and then used the Up arrow to retrieve her previous command. She then modified it until the following command appeared.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$books.bookinfo.booklist.book | % {$_.subjects.subject} | sort displayname | group displayname -NoElement&lt;/p&gt;
&lt;p&gt;She turned the monitor to me, and the output that is shown in the image that follows was the result.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2210.HSG_2D00_3_2D00_26_2D00_12_2D00_02.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2210.HSG_2D00_3_2D00_26_2D00_12_2D00_02.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;That was pretty complicated. Can&amp;rsquo;t we do something easier? For instance, I want to find all the Perry Mason novels,&amp;rdquo; she requested.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;That one really is easy. Retrieve your command that lists all the titles. Because the titles are all strings, pipe that to the &lt;b&gt;Where-Object &lt;/b&gt;and use the &lt;b&gt;Match&lt;/b&gt;&lt;i&gt; &lt;/i&gt;operator to look for &lt;b&gt;Mason&lt;/b&gt;&lt;i&gt;,&lt;/i&gt;&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;This time the Scripting Wife did not hesitate. She retrieved her next to last command by pressing the Up arrow twice. Then she added a pipe character, used the question mark (?) as an alias for the &lt;b&gt;Where-Object&lt;/b&gt;. She added the &lt;b&gt;Match&lt;/b&gt;&lt;i&gt; &lt;/i&gt;operator and looked for the word &lt;b&gt;Mason&lt;/b&gt;&lt;i&gt;.&lt;/i&gt; Her completed command is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$books.bookinfo.booklist.book | % {$_.mainsection.title} | ? { $_ -match &amp;#39;mason&amp;#39;}&lt;/p&gt;
&lt;p&gt;&amp;ldquo;This is more like it. Here take a look,&amp;rdquo; she said as she turned the monitor towards me.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/3240.HSG_2D00_3_2D00_26_2D00_12_2D00_03.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/3240.HSG_2D00_3_2D00_26_2D00_12_2D00_03.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;That is all I really wanted to see right now. I think I am going to head to the mall with a couple of my friends. You have been keeping me too busy around here, and I need a break,&amp;rdquo; she said as she jumped up and headed towards the door.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Don&amp;rsquo;t forget&amp;hellip;registration for the 2012 Scripting Games is open,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;I will register tomorrow,&amp;rdquo; she said.&lt;/p&gt;
&lt;p&gt;And she was gone.&lt;/p&gt;
&lt;p&gt;Join us tomorrow when I imagine the Scripting Wife will register for the 2012 Scripting Games.&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3488322" width="1" height="1" alt="" /&gt;</description></item><item><title>The Scripting Wife Learns to Use PowerShell to Work with XML</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/03/25/the-scripting-wife-learns-to-use-powershell-to-work-with-xml.aspx</link><pubDate>Sun, 25 Mar 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:15526</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: In this blog, the Scripting Wife learns how to use Windows PowerShell to work with an XML file.&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. It is only eight days until the kickoff of the 2012 Scripting Games. Tomorrow the signup for the 2012 Scripting Games goes live. I have finished all the meetings, and everything is now in readiness state for the big event. I am sitting in the kitchen sipping a cup of English Breakfast tea, and looking over the chapter that I completed last night in my &lt;a href="http://shop.oreilly.com/product/0790145337382.do" target="_blank"&gt;Windows PowerShell 3.0 Step by Step&lt;/a&gt; book. I close my eyes to think about one particular sentence when all of a sudden I feel a presence come into the room.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;If you are going to sleep, then you should get up from the table,&amp;rdquo; the Scripting Wife chided.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;I am not sleeping, I am deep in thought,&amp;rdquo; I replied.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Then I am in deep trouble,&amp;rdquo; the Scripting Wife rejoined.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;What are you doing in here? The sun is still up,&amp;rdquo; I joked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Very funny. In fact, it is so funny I forgot to laugh,&amp;rdquo; she said. &amp;ldquo;Actually, since you are not doing anything, I need to you tell me about XML.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Say what?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;XML. I need to know how to read an XML file,&amp;rdquo; she repeated.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;You are kidding.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Nope. I exported my book database, and it saved as an XML file. I need to know how to read it,&amp;rdquo; the Scripting Wife explained.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;OK. That makes sense. In Windows PowerShell, it is really easy. How do you read a plain text file?&amp;rdquo; I asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;I use the &lt;b&gt;Get-Content &lt;/b&gt;cmdlet,&amp;rdquo; she said.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;It is the same thing, except that you use the letters XML inside square brackets. Open up Windows PowerShell on your computer, and then use &lt;b&gt;Get-Content &lt;/b&gt;to read the XML file.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;The Scripting Wife stores her books.xml file in the FSO directory on her C:\ drive. Therefore, her command looks like this:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;[xml]$books = Get-Content C:\fso\books.xml&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Now look at what is contained in the &lt;b&gt;$books&lt;/b&gt; variable,&amp;rdquo; I suggested.&lt;/p&gt;
&lt;p&gt;The Scripting Wife typed the &lt;b&gt;$books&lt;/b&gt; variable on its own line in the Windows PowerShell console. The command and its associated output are shown in the image that follows.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/1440.HSG_2D00_3_2D00_25_2D00_12_2D00_01.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/1440.HSG_2D00_3_2D00_25_2D00_12_2D00_01.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Now look at the &lt;b&gt;XML&lt;/b&gt; property,&amp;rdquo; I suggested.&lt;/p&gt;
&lt;p&gt;The Scripting Wife typed the following command.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$books.xml&lt;/p&gt;
&lt;p&gt;&amp;ldquo;OK. Now look at the &lt;b&gt;BookInfo&lt;/b&gt; property.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;She typed the command that appears here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$books.bookinfo.&lt;/p&gt;
&lt;p&gt;The command to examine the &lt;b&gt;XML&lt;/b&gt; property and the command to look at the &lt;b&gt;BookInfo&lt;/b&gt; property are shown in the image that follows, along with the associated output.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2625.HSG_2D00_3_2D00_25_2D00_12_2D00_02.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2625.HSG_2D00_3_2D00_25_2D00_12_2D00_02.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;What is that booklist?&amp;rdquo; the Scripting Wife asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;I don&amp;rsquo;t know. Why don&amp;rsquo;t you access it and see.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;The Scripting Wife used the Up arrow to retrieve the previous command, and added &lt;b&gt;BookList&lt;/b&gt;&lt;i&gt; &lt;/i&gt;to the end of the command and pressed ENTER. Here is her command:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$books.bookinfo.booklist&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Well, it looks like a bunch of books,&amp;rdquo; she replied as she turned her monitor to me so I could see. The image that follows shows her Windows PowerShell console.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/7607.HSG_2D00_3_2D00_25_2D00_12_2D00_03.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/7607.HSG_2D00_3_2D00_25_2D00_12_2D00_03.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Why don&amp;rsquo;t you keep going&amp;hellip;But this time, pipe the output to &lt;b&gt;More&lt;/b&gt;&lt;i&gt; &lt;/i&gt;just in case you get all of your books rolling by,&amp;rdquo; I suggested.&lt;/p&gt;
&lt;p&gt;The Scripting Wife once again used the Up arrow to retrieve her previous command, added &lt;b&gt;Book&lt;/b&gt;&lt;i&gt; &lt;/i&gt;to the end of the command, and piped the output to &lt;b&gt;More&lt;/b&gt;&lt;i&gt;. &lt;/i&gt;The command is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$books.bookinfo.booklist.book | more&lt;/p&gt;
&lt;p&gt;Sure enough, after a few seconds, the book information began to scroll. The first little bit is shown here.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/3010.HSG_2D00_3_2D00_25_2D00_12_2D00_04.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/3010.HSG_2D00_3_2D00_25_2D00_12_2D00_04.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Well that looks interesting, but how can I find more information about a book?&amp;rdquo; she asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Why don&amp;rsquo;t you index into the collection? Choose the first book,&amp;rdquo; I suggested.&lt;/p&gt;
&lt;p&gt;The Scripting Wife thought for a few seconds, and then she used the Up arrow to retrieve her previous command and then added the index operator. Her command is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$books.bookinfo.booklist.book[0]&lt;/p&gt;
&lt;p&gt;The output from the previous command is shown here.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6327.HSG_2D00_3_2D00_25_2D00_12_2D00_05.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6327.HSG_2D00_3_2D00_25_2D00_12_2D00_05.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;You can use the same techniques to find information about the subjects of the book, and to find information about the title, plot, and other information.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;The Scripting Wife typed the commands that follow. As she examined the output from each command she continued to burrow deeper and deeper into the data structure of the first book.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$books.bookinfo.booklist.book[0].subjects&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$books.bookinfo.booklist.book[0].subjects.subject&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$books.bookinfo.booklist.book[0].mainsection&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$books.bookinfo.booklist.book[0].mainsection.authors&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$books.bookinfo.booklist.book[0].mainsection.authors.author&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$books.bookinfo.booklist.book[0].mainsection.authors.author.person&lt;/p&gt;
&lt;p&gt;Finally, she sat back and examined her handiwork. The output from these commands are shown in the image that follows.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8032.HSG_2D00_3_2D00_25_2D00_12_2D00_06.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8032.HSG_2D00_3_2D00_25_2D00_12_2D00_06.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3488316" width="1" height="1" alt="" /&gt;</description></item><item><title>Learn How to Manually Create a CSV File in PowerShell</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/03/24/learn-how-to-manually-create-a-csv-file-in-powershell.aspx</link><pubDate>Sat, 24 Mar 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:15518</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: The Scripting Wife learns how to manually create a CSV file in this beginner Windows PowerShell blog.&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. Ah, it is the weekend. It seems that this week has been rather long, although I know that is a misconception because each week only has 168 hours in it. I guess it is kind of like one of the characters in &lt;a href="http://en.wikipedia.org/wiki/One_Flew_Over_the_Cuckoo%27s_Nest_(novel)" target="_blank"&gt;One Flew Over the Cuckoo&amp;#39;s Nest&lt;/a&gt;, a book I read in high school. They assumed that the staff sped up the clock when they were having fun, and slowed down the clock when they were doing things they disliked as a way of punishing them. Anyway, this morning I am in my office busily working on my new Microsoft Press book, &lt;a href="http://shop.oreilly.com/product/0790145337382.do" target="_blank"&gt;Windows PowerShell 3.0 Step by Step&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Hmmm&amp;hellip;I like the new &lt;b&gt;Where&lt;/b&gt; clause syntax in Windows PowerShell&amp;nbsp;3.0,&amp;rdquo; I said to myself.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;So what is a CSV?&amp;rdquo; said the Scripting Wife as she jolted me from my mental revelry.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Is it a model of a car?&amp;rdquo; I facetiously queried.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;No. Not even close, Script Monkey,&amp;rdquo; she shot back, &amp;ldquo;Try again.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;OK. It is a comma-separated value type of text file,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Correct. Give the Script Monkey a peanut,&amp;rdquo; she said.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;So why do you want to know about CSV files?&amp;rdquo; I asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Because silly, it is in the &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/05/2012-scripting-games-study-guide-a-resource-for-learning-powershell.aspx" target="_blank"&gt;2012 Scripting Games Study Guide&lt;/a&gt;, and the games start in exactly nie days,&amp;rdquo; she replied.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;OK. What do you want to know about CSV files?&amp;rdquo; I asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Everything.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;I see. Well, you could start by &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/tags/windows+powershell/csv+and+other+delimited+files/" target="_blank"&gt;reading all the CSV blogs&lt;/a&gt; I have written on the Hey, Scripting Guy! Blog.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;I could, but I want you to teach me. After all, you are the world famous Scripting Guy.&amp;rdquo;&lt;/p&gt;
&lt;h1&gt;Manually creating a CSV File&lt;/h1&gt;
&lt;p&gt;&amp;ldquo;What I am going to show you today is how to create CSV file manually. The first thing you need to do is to create a here string,&amp;rdquo; I said while turning my laptop screen so she could see.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;A hear string? What is this? Like a symphony or something?&amp;rdquo; she quipped.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Try to stay with me. A here string allows you to create a string without worrying about any sort of formatting like quotation marks and commas, which are two things that CSV files really love, and that Windows PowerShell in general balks at.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;OK. So how do I create this magical here string?&amp;rdquo; she asked with obvious signs of skepticism.&lt;/p&gt;
&lt;h1&gt;Use @&amp;rdquo; to begin and &amp;ldquo;@ to end a here string&lt;/h1&gt;
&lt;p&gt;&amp;ldquo;The first thing I do is use a variable to hold the resulting string. Then I begin the here string with the at plus quotation symbols (@&amp;rdquo;). Then I immediately press ENTER, and I add my column headings. A comma separates each heading, and then I again press ENTER. Now I add the value for each column and I use a comma to separate the values.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;That is why it is called a comma-separated value file?&amp;rdquo; she asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Exactly. When all of the rows of data are typed, press ENTER again, and I close the here string with the quotation mark plus at symbols (&amp;ldquo;@) and again press ENTER.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;OK,&amp;rdquo; she said somewhat skeptically.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Why don&amp;rsquo;t you give it a try,&amp;rdquo; I said as I turned the laptop over to her.&lt;/p&gt;
&lt;p&gt;The Scripting Wife opened the Windows PowerShell console, and typed the following on the first line. In the following code, &amp;lt;enter&amp;gt; represents the ENTER or Return key (ASCII 13) (8 ).&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$csv = @&amp;quot;&amp;lt;enter&amp;gt;&lt;/p&gt;
&lt;p&gt;Next she added the column headings by typing the following.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;quot;index&amp;quot;,&amp;quot;name&amp;quot;&amp;lt;enter&amp;gt;&lt;/p&gt;
&lt;p&gt;After the columns were added, the Scripting Wife added the data. This is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;1,&amp;quot;ed&amp;quot;&amp;lt;enter&amp;gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;2,&amp;quot;teresa&amp;quot;&amp;lt;enter&amp;gt;&lt;/p&gt;
&lt;p&gt;Finally, she closed the here string.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;quot;@&amp;lt;enter&amp;gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;lt;enter&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Now you need to write your CSV data to a file,&amp;rdquo; I said. &amp;ldquo;When you are done, use the &lt;b&gt;Import-CSV &lt;/b&gt;cmdlet to read it back.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Oh, I can use redirection to create the file,&amp;rdquo; she said.&lt;/p&gt;
&lt;p&gt;The Scripting Wife quickly typed the following.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$csv &amp;gt;&amp;gt;c:\fso\mycsv.csv&amp;lt;enter&amp;gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Import-Csv C:\fso\mycsv.csv&amp;lt;enter&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Very good. Now store the CSV information back into a new variable, and access the first element in the file,&amp;rdquo; I said.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Huh? Do you mean for me to use square brackets and see the first row of information?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Yeah.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Then why don&amp;rsquo;t you just say that?&amp;rdquo; she chided.&lt;/p&gt;
&lt;p&gt;The Scripting Wife typed the following.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$mycsv = Import-Csv C:\fso\mycsv.csv&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$mycsv[0]&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Do you see that your first row of data contains two items&amp;mdash;the index and the name?&amp;rdquo; I asked.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Yes.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;You can use dotted notation to access the specific column of information. These act just like properties. Why don&amp;rsquo;t you get the name from the second record?&amp;rdquo; I suggested.&lt;/p&gt;
&lt;p&gt;The Scripting Wife typed the following.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$mycsv[1].name&lt;/p&gt;
&lt;p&gt;&amp;ldquo;So how does all this look?&amp;rdquo; the Scripting Wife asked as she turned the monitor over so I could see.&lt;/p&gt;
&lt;p&gt;As I looked at the screen, I beamed. &amp;ldquo;It is perfect.&amp;rdquo; The screen is shown in the image that follows.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2055.HSG_2D00_3_2D00_24_2D00_12_2D00_01.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2055.HSG_2D00_3_2D00_24_2D00_12_2D00_01.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Cool,&amp;rdquo; she said as she jumped up and prepared to leave.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;By the way, how did you do on the new 2012 Scripting Games PowerShell Quiz?&amp;rdquo; I asked.&lt;/p&gt;
&lt;p&gt;But by this time she was long gone. Perhaps she did not hear. Or maybe she did&amp;hellip;&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3488314" width="1" height="1" alt="" /&gt;</description></item></channel></rss>