<?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 'Getting Started' and 'Strings'</title><link>http://powershell.com/cs/search/SearchResults.aspx?q=app:weblogs&amp;tag=Getting+Started,Strings&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tags 'Getting Started' and 'Strings'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 (Build: 30929.2835)</generator><item><title>Variable Substitution in a PowerShell Script Block</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2013/05/22/variable-substitution-in-a-powershell-script-block.aspx</link><pubDate>Wed, 22 May 2013 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:23655</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;strong style="font-size:12px;"&gt;Summary&lt;/strong&gt;&lt;span style="font-size:12px;"&gt;: Microsoft Scripting Guy, Ed Wilson, talks about performing variable substitution inside a Windows PowerShell script block.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/q-sm.jpg" alt="Hey, Scripting Guy! Question" /&gt;&amp;nbsp;Hey, Scripting Guy! &amp;nbsp;I am trying to create a command. The command contains variables that I would like to assign prior to creating the command. However, when I get to the script block portion of my code, it does not do the variable substitution for the value the way an expanding string normally works. Can you help me?&lt;/p&gt;
&lt;p&gt;&amp;mdash;SW&lt;/p&gt;
&lt;p&gt;&lt;img src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg" alt="Hey, Scripting Guy! Answer" /&gt;&amp;nbsp;Hello SW,&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. Last week at the first ever Northern Virginia PowerShell User Group, the Scripting Wife and I found a great little tea shop. I can&amp;rsquo;t help but thinking about the scone I got there&amp;mdash;it was pretty good.&lt;/p&gt;
&lt;h2&gt;Expanding variable values&lt;/h2&gt;
&lt;p&gt;One of the really cool things about Windows PowerShell is the expanding strings&amp;mdash;I absolutely love them. It surely beats having to do lots of string concatenation like I had to do back in the VBScript days. To illustrate this technique, I assign a string to the value of a variable&amp;mdash;in this case, the &lt;strong&gt;$a&lt;/strong&gt; variable. I look at the value contained in the variable. This code is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $a = &amp;quot;This is a string&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $a&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;This is a string&lt;/p&gt;
&lt;p&gt;Now, by using the expanding string, I can see the value that is contained inside the &lt;strong&gt;$a&lt;/strong&gt; variable. To suppress the variable expansion, I escape it with the grave accent character as shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; &amp;quot;The value of `$a is $a&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;The value of $a is This is a string&lt;/p&gt;
&lt;h2&gt;The problem with a script block&lt;/h2&gt;
&lt;p&gt;If, on the other hand, I want to expand the value of a variable inside a script block, it does not work. This is shown in the code that follows.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $a = &amp;quot;This is a string&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $a&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;This is a string&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $b = {&amp;quot;The value of `$a is $a&amp;quot;}&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $b&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;quot;The value of `$a is $a&amp;quot;&lt;/p&gt;
&lt;h2&gt;Solving the problem with variable expansion in a script block&lt;/h2&gt;
&lt;p&gt;The solution to expanding a variable inside a script block is to do two things. First create the script block as an expanding string. This is shown here:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $a = &amp;quot;This is a string&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $a&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;This is a string&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $b = &amp;quot;The value of `$a is $a&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $b&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;The value of $a is This is a string&lt;/p&gt;
&lt;p&gt;Now, I use the static &lt;strong&gt;Create&lt;/strong&gt;&lt;em&gt; &lt;/em&gt;method from the &lt;strong&gt;[scriptblock]&lt;/strong&gt; class. This will create a script block. To do this, I use the &lt;strong&gt;[scriptblock]&lt;/strong&gt; class and then call the &lt;strong&gt;Create&lt;/strong&gt;&lt;em&gt; &lt;/em&gt;method while passing the string contained in the &lt;strong&gt;$b&lt;/strong&gt; variable. This is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; [scriptblock]::Create($b)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;The value of $a is This is a string&lt;/p&gt;
&lt;p&gt;I can confirm that it is in fact a script block by piping the results to the &lt;strong&gt;Get-Member&lt;/strong&gt; cmdlet as shown here:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; [scriptblock]::Create($b) | gm&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp; TypeName: System.Management.Automation.ScriptBlock&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Name&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MemberType Definition&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;----&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ---------- ----------&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;CheckRestrictedLanguage Method&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; void CheckRestrictedLanguage(System.Collection...&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Equals&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Method&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; bool Equals(System.Object obj)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;GetHashCode&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Method&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int GetHashCode()&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;GetNewClosure&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Method&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; scriptblock GetNewClosure()&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;GetObjectData&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Method&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; void GetObjectData(System.Runtime.Serializatio...&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;GetPowerShell&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Method&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; powershell GetPowerShell(Params System.Object[...&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;GetSteppablePipeline&amp;nbsp;&amp;nbsp;&amp;nbsp; Method&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Management.Automation.SteppablePipeline...&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;GetType&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Method&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; type GetType()&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Invoke&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Method&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Collections.ObjectModel.Collection[psob...&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;InvokeReturnAsIs&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Method&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Object InvokeReturnAsIs(Params System.O...&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;ToString&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Method&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string ToString()&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Ast&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Property&amp;nbsp;&amp;nbsp; System.Management.Automation.Language.Ast Ast ...&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Attributes&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Property&amp;nbsp;&amp;nbsp; System.Collections.Generic.List[System.Attribu...&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;File&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Property&amp;nbsp;&amp;nbsp; string File {get;}&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;IsFilter&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Property&amp;nbsp;&amp;nbsp; bool IsFilter {get;set;}&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Module&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Property&amp;nbsp;&amp;nbsp; psmoduleinfo Module {get;}&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;StartPosition&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Property&amp;nbsp;&amp;nbsp; System.Management.Automation.PSToken StartPosi...&lt;/p&gt;
&lt;p&gt;Now, the cool thing about this is that I can also store the script block into another variable. This is shown here:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$sb = [scriptblock]::Create($b)&lt;/p&gt;
&lt;p&gt;After I have stored the script block into the variable, I can also call any of the methods or properties of the script block. For example, here is the AST:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $sb.Ast&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;ParamBlock&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; :&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;BeginBlock&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; :&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;ProcessBlock&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; :&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;EndBlock&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : The value of $a is This is a string&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;DynamicParamBlock&amp;nbsp; :&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;ScriptRequirements :&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Extent&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : The value of $a is This is a string&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Parent&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; :&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:12px;"&gt;It is not horribly exciting in this example, but for more complex code, it is definitely exciting stuff. We have a great Hey, Scripting Guy! Blog post written by Bartek Bielawski, which offers several good ideas for further exploration: &lt;/span&gt;&lt;a style="font-size:12px;" href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/09/26/learn-how-it-pros-can-use-the-powershell-ast.aspx" target="_blank"&gt;Learn How IT Pros Can Use the PowerShell AST&lt;/a&gt;&lt;span style="font-size:12px;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;SW, that is all there is to using variable expansion and substitution in a Windows PowerShell script block.&amp;nbsp; Join me tomorrow when I will talk about way cool Windows PowerShell stuff.&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;strong&gt;Ed Wilson, Microsoft Scripting Guy&lt;/strong&gt;&lt;span style="font-size:12px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3572439" width="1" height="1" alt="" /&gt;</description></item><item><title>Weekend Scripter: Handle the PowerShell Pipeline and Use the Console</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/04/29/weekend-scripter-handle-the-powershell-pipeline-and-use-the-console.aspx</link><pubDate>Sun, 29 Apr 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:16265</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: Microsoft Scripting Guy, Ed Wilson, talks about handling output to the Windows PowerShell console while using the pipeline.&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I are enjoying Virginia Beach. We have met some really cool and smart people here at the &lt;a href="http://minasiconference.wordpress.com/speakers/" target="_blank"&gt;Mark Minasi Conference&lt;/a&gt;. We had a chance to go to the huge naval museum here. It was cool. In the museum, they have different types of exhibits. Some of them have a button that you push, and you listen to it, then you go to the next exhibit. Others have drawings and diagrams that you read, and then you go to the next exhibit. Still others have brochures that you can skim and take with you when you leave the museum.&lt;/p&gt;
&lt;p&gt;In a way, Windows PowerShell output behaves the save way. Some things we write to the Windows PowerShell console, others we write to files. Some things display to the console and write to files, and still others appear and then they are gone, never to return. It is the ease-of-use plus the diversity of options for creating output from inside Windows PowerShell that is a source of confusion for beginning (and even for some advanced) Windows PowerShell scripters.&amp;nbsp;Simply put&amp;hellip;&lt;/p&gt;
&lt;p&gt;Like the child who comes home from the grocery store with a loaf of bread and 12 candy bars, we often do not make the best choices. Just like the child with the dozen candy bars and a loaf of bread, some of the options seem too compelling. We get over-awed with the choices&amp;mdash;in short, we zone out into a sort of geek trance. In the following command, I use the &lt;b&gt;Get-Command&lt;/b&gt; cmdlet, the &lt;b&gt;Sort-Object&lt;/b&gt; cmdlet, the &lt;b&gt;Select-Object&lt;/b&gt; cmdlet, and the &lt;b&gt;Format-Wide&lt;/b&gt; cmdlet to produce a list of the more common output and formatting cmdlets. Twenty cmdlets appear on this list (&lt;b&gt;Measure-Command &lt;/b&gt;tells me that). The dizzying array of choices are shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; gcm -Verb write, out, tee, format | sort verb | select name&amp;nbsp; | fw -a&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Format-Table&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Format-Wide&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Format-Custom&amp;nbsp;&amp;nbsp;&amp;nbsp; Format-List&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Out-Null&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Out-Printer&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Out-String&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Out-Host&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Out-Default&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Out-File&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Out-GridView&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Tee-Object&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Write-Progress&amp;nbsp;&amp;nbsp; Write-Output&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Write-Warning&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Write-Verbose&amp;nbsp;&amp;nbsp;&amp;nbsp; Write-Error&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Write-Debug&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Write-Host&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Write-EventLog&lt;/p&gt;
&lt;h1&gt;The easiest way to write output&lt;/h1&gt;
&lt;p&gt;The easiest way to write output is not to use any of the twenty cmdlets listed previously. Rather, it is to place the output into a string. There are two types of strings in Windows PowerShell, the expanding string and the literal string. The difference is twofold; an expanding string uses double quotation marks, and a literal string uses single quotation marks. The practical application of these different types of strings is also twofold. A literal string means that &lt;i&gt;what you type is what you get. &lt;/i&gt;This is, in fact, the way that strings worked in other languages (for example in VBScript). The difference, with VBScript anyway, is that the string indicator was the double quotation mark.&lt;/p&gt;
&lt;h2&gt;Using literal strings&lt;/h2&gt;
&lt;p&gt;The following example illustrates how a literal string works. It uses single quotation marks and contains a variable. When the string displays to the Windows PowerShell console, the variable appears exactly as typed. That is, the variable does not expand to display the value that the variable contains.&lt;/p&gt;
&lt;p&gt;In the following example, in the first line, a string assigns to the variable &lt;b&gt;$a&lt;/b&gt;. The double quotation marks do this, but you could just as easily (and with no difference in the outcome of the code) use single quotation marks for this string. This is because the string contains no variables to expand. In the second line of code, a pair of single quotation marks delineates the string. This time, the string contains two variables. The output displays to the Windows PowerShell concole, but the values contained inside the &lt;b&gt;$a&lt;/b&gt; variables do not unravel. Instead, only the literal &lt;b&gt;$a&lt;/b&gt; appears on the outputted line.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $a = &amp;quot;this is a string contained in a variable&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; &amp;#39;The value of the $a variable is actually $a&amp;#39;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;The value of the $a variable is actually $a&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt;&lt;/p&gt;
&lt;h2&gt;Using expanding strings&lt;/h2&gt;
&lt;p&gt;If you replace the single quotation marks in the second line of code with double quotation marks, the difference is dramatic. For each of the two locations where the &lt;b&gt;$a&lt;/b&gt; variable appears in the code, the value contained in the variable replaces the literal variable name. The resulting output is not very useful, but it is different.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; &amp;quot;The value of the $a variable is actually $a&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;The value of the this is a string contained in a variable variable is actually this&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;is a string contained in a variable&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt;&lt;/p&gt;
&lt;h2&gt;Suppress automatic expansion&lt;/h2&gt;
&lt;p&gt;What you might want to do is to use the expanding or unraveling feature of the expanding string for the second appearance of the &lt;b&gt;$a&lt;/b&gt; variable, but suppress it for the first appearance. To suppress the expansion of the first variable, use the grave accent (also called back-tick) character (&lt;b&gt;`&lt;/b&gt;) immediately prior to the first appearance of the &lt;b&gt;$a&lt;/b&gt; variable. This is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; &amp;quot;The value of the `$a variable is actually $a&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;The value of the $a variable is actually this is a string contained in a variable&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt;&lt;/p&gt;
&lt;p&gt;These examples of using literal and expanding strings are shown with their associated output 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/3034.wes_2D00_4_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/3034.wes_2D00_4_2D00_29_2D00_12_2D00_01.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;Writing output to the console&lt;/h1&gt;
&lt;p&gt;Most of the time, beginners who are learning Windows PowerShell use the &lt;b&gt;Write-Host&lt;/b&gt; cmdlet to display output to the Windows PowerShell console. One reason for doing this is that people coming from a VBScript background (where they used &amp;ldquo;Wscript.Echo&amp;rdquo; to write output) feel that they need to do something similar to this. They find &lt;b&gt;Write-Host&lt;/b&gt;, and they see it as the direct replacement. Next, they proceed with a line-by-line adaptation of their previous VBScript to the &lt;i&gt;new &lt;/i&gt;Windows PowerShell script. Unfortunately, this does not take advantage of an inherent Windows PowerShell strength &amp;mdash;it works with objects, not strings.&lt;/p&gt;
&lt;p&gt;There are occasions when using &lt;b&gt;Write-Host&lt;/b&gt; is the right thing to do. For example, if you are at the end of a command and you want to take advantage of the &lt;b&gt;Foreground&lt;/b&gt;&lt;i&gt; &lt;/i&gt;or the &lt;b&gt;Background&lt;/b&gt;&lt;i&gt; &lt;/i&gt;color abilities of the &lt;b&gt;Write-Host&lt;/b&gt; cmdlet, the command makes sense. On the other hand, if you are not using the color capabilities of the &lt;b&gt;Write-Host&lt;/b&gt; cmdlet, it makes sense to use the &lt;b&gt;Write-Output&lt;/b&gt; cmdlet instead. The reason is that &lt;b&gt;Write-Host&lt;/b&gt; is a terminal cmdlet&amp;mdash;that is, after you use it, you can do nothing else. If you try to pipe the output from the &lt;b&gt;Write-Host&lt;/b&gt; cmdlet to a text file, no error generates, but the text file is empty. This is because no objects return from the &lt;b&gt;Write-Host&lt;/b&gt; cmdlet. The commands 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/7317.wes_2D00_4_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/7317.wes_2D00_4_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;The &lt;b&gt;Out-File&lt;/b&gt; cmdlet creates the writehost.txt file, but because &lt;b&gt;Write-Host&lt;/b&gt; passes no objects along the pipeline, the text file contains no text. The empty file 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/6886.wes_2D00_4_2D00_29_2D00_12_2D00_03.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/400x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6886.wes_2D00_4_2D00_29_2D00_12_2D00_03.png" alt="Image of file" title="Image of file" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;Choosing both console and file&lt;/h1&gt;
&lt;p&gt;You do not need to write complicated duplicate code to write data to the Windows PowerShell console and to write to a file. In fact, you do not need to store the data in an intermediate variable&amp;mdash;it can be done with a single command. The cmdlet to use is &lt;b&gt;Tee-Object&lt;/b&gt;. The &lt;b&gt;Tee-Object &lt;/b&gt;cmdlet accepts a single input and then splits the output to multiple locations. It can display data to the Windows PowerShell console and write it to a file. It can also display to the console and write back to a variable. The basic syntax is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;quot;output from the Tee-Object&amp;quot; | Tee-Object -FilePath c:\fso\tee.txt&lt;/p&gt;
&lt;p&gt;The command and its associated output are shown in the following image.&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/8400.wes_2D00_4_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/8400.wes_2D00_4_2D00_29_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;A quick check of the text file reveals that the text file does indeed contain the text output to the console. The Tee.txt file 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/1541.wes_2D00_4_2D00_29_2D00_12_2D00_05.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/400x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/1541.wes_2D00_4_2D00_29_2D00_12_2D00_05.png" alt="Image of file" title="Image of file" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You are not limited to only sending the output to the Windows PowerShell console and a file, or to a variable and the console. This is because the cmdlet splits the output. You can use a variable to hold part of the output, and you can still send it to a text file. When you have the output in a variable, you can easily display the contents of the variable to the console. This is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $t = &amp;quot;output from the Tee-Object. Test 2&amp;quot; | Tee-Object -FilePath c:\fso\tee2.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;txt&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $t&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;output from the Tee-Object. Test 2&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt;&lt;/p&gt;
&lt;p&gt;That is about all there is for now. 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=3493515" width="1" height="1" alt="" /&gt;</description></item><item><title>Weekend Scripter: Learn How to Handle String Output from Within PowerShell</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/04/28/weekend-scripter-learn-how-to-handle-string-output-from-within-powershell.aspx</link><pubDate>Sat, 28 Apr 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:16259</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: Microsoft Scripting Guy, Ed Wilson, discusses several different ways to handle string output from inside Windows PowerShell.&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. It is official&amp;mdash;the 2012 Scripting Games are finally over (actually they were over yesterday, and I promised not to say any more about the games). Today the Scripting Wife and I are heading to Virginia Beach where I will speak at the &lt;a href="http://minasiconference.wordpress.com/minasi-2012-timetable/" target="_blank"&gt;Mark Minasi Conference&lt;/a&gt;. If you happen to be in the area, stop by and check it out. It will be a great conference, and I am really looking forward to it.&lt;/p&gt;
&lt;p&gt;One of the things that beginners have a problem with is formatting output. In particular forming strings that contain data. One of the things Windows PowerShell does really well is handle input and output seamlessly. For example, many cmdlets automatically emit data. For example, the &lt;b&gt;Get-Process&lt;/b&gt; cmdlet returns a nicely formatted table as 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/1300.Wes_2D00_4_2D00_28_2D00_12_2D00_1.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/1300.Wes_2D00_4_2D00_28_2D00_12_2D00_1.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;But things begin to go downhill really fast. Suppose you have a process named &lt;i&gt;Notepad &lt;/i&gt;and you want to display the name and the process ID (PID). One way to approach this task is to use the &lt;b&gt;Get-Process &lt;/b&gt;cmdlet to retrieve the instance of the &lt;i&gt;Notepad &lt;/i&gt;process with which you are interested in working. Next you could store this process object into a variable, and then use expanding quotation marks (double quotation marks as opposed to literal quotation marks, which are single quotation marks) to expand the &lt;b&gt;Name&lt;/b&gt; and &lt;b&gt;ID&lt;/b&gt; properties. This technique is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; notepad&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $n = Get-Process notepad&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; &amp;quot;the name is $n.Name and the PID is $n.Id&amp;quot;&lt;/p&gt;
&lt;p&gt;The problem is that when the string displays to the Windows PowerShell console, the object &lt;i&gt;unravels &lt;/i&gt;and you do not get the property value that you might have been expecting to obtain. Instead, you get a jumble of letters that do not seem to make any sense. These commands and their associated output 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/5270.wes_2D00_4_2D00_28_2D00_12_2D00_2.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5270.wes_2D00_4_2D00_28_2D00_12_2D00_2.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;There are several ways to suppress the unraveling of the objects. One way is to use a subexpression. A subexpression is comprised of a dollar sign, and a pair of parentheses. The subexpression forces the evaluation of the object/property value and then returns the value of the property back to the string for display. A pair of parentheses causes Windows PowerShell to evaluate an expression prior to displaying the output. Therefore, it can display the contents of things like variables. A subexpression takes this a step further and causes the evaluation of the value of a property from an object. The syntax of this is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; notepad&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $n = Get-Process notepad&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; &amp;quot;the name is $($n.Name) and the PID is $($n.Id)&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;the name is notepad and the PID is 2816&lt;/p&gt;
&lt;p&gt;The command to start notepad, retrieve an instance of the object and store it in the &lt;b&gt;$n&lt;/b&gt; variable, and display the property values is shown here 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/7140.wes_2D00_4_2D00_28_2D00_12_2D00_3.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/7140.wes_2D00_4_2D00_28_2D00_12_2D00_3.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Another approach to this problem is to use concatenation. The concatenation operator is the plus sign (+). Concatenation glues the output together so it will display on the same line. By using concatenation, you place your string values inside quotation marks. You then close the quotation marks and call the object/property to display it. You then open another pair of quotation marks to add your additional string data. You continue with this pattern until you have completed displaying your data.&lt;/p&gt;
&lt;p&gt;This approach to merging data from property values and strings is essentially unchanged from the earliest VBScript days. The big trick with this technique is that you must remember to include spacing between the string and the property value. If you do not, the concatenation operator will glue your string together. This approach is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; notepad&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $n = Get-Process notepad&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; &amp;quot;the name is &amp;quot; + $n.name + &amp;quot; and the PID is &amp;quot; + $n.id&lt;/p&gt;
&lt;p&gt;The commands and the output associated with the commands 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/1768.wes_2D00_4_2D00_28_2D00_12_2D00_4.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/1768.wes_2D00_4_2D00_28_2D00_12_2D00_4.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A different problem exists when working with multiple objects. In this situation, the properties appear to disappear. This is because the process objects (in this example) hide inside an array and when attempting to access directly the property values. You must first deal with the array, and then you can work with the property values. If you do not first deal with the array, when you attempt to display the property values, nothing appears in the output. This situation is shown in the image 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/5661.wes_2D00_4_2D00_28_2D00_12_2D00_5.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5661.wes_2D00_4_2D00_28_2D00_12_2D00_5.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Because there are multiple objects in the array, it is necessary to walk through the array so that the individual values from the properties of the individual objects are accessible. The easiest way to accomplish this is to pipe the array of objects to the &lt;b&gt;Foreach-Object&lt;/b&gt; cmdlet. In the script block portion of the &lt;b&gt;Foreach-Object&lt;/b&gt; cmdlet, add the code from the previous examples. This technique is shown here (this example uses the % symbol, which is an alias for the &lt;b&gt;Foreach-Object&lt;/b&gt; cmdlet).&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; 1..3 | % {notepad}&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $n = Get-Process notepad&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $n | % {&amp;quot;the name is &amp;quot; + $_.name + &amp;quot; and the PID is &amp;quot; + $_.id}&lt;/p&gt;
&lt;p&gt;The code, from this technique, along with the associated output appears 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/2046.wes_2D00_4_2D00_28_2D00_12_2D00_6.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2046.wes_2D00_4_2D00_28_2D00_12_2D00_6.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You can use a similar technique to using the &lt;b&gt;Foreach-Object &lt;/b&gt;cmdlet by using the &lt;i&gt;ForEach &lt;/i&gt;language statement to walk through the array and display the property values. The &lt;i&gt;ForEach &lt;/i&gt;language statement is a bit more complex to use than the &lt;b&gt;Foreach-Object &lt;/b&gt;cmdlet, but it has the advantage of being a bit easier to read, and of being generally faster due to not involving the overhead of the pipeline. The use of the &lt;i&gt;ForEach &lt;/i&gt;language statement to walk through the array is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; 1..3 | % {start-process notepad -WindowStyle minimized}&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $n = Get-Process notepad&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; foreach($a in $n) {&amp;quot;the name is &amp;quot; + $a.name + &amp;quot; and the PID is &amp;quot; + $a.id}&lt;/p&gt;
&lt;p&gt;Because an array contains the objects, it is possible to index the array to display the property values. One easy way to do this is to use the same type of range operator that created the three instances of &lt;i&gt;Notepad&lt;/i&gt; in the first place. When it is created, the &lt;b&gt;$n&lt;/b&gt; variable stores the objects that are returned by the &lt;b&gt;Get-Process &lt;/b&gt;cmdlet. The range operator passes the numbers that become the index indicators to select the specific element from the array. The dotted notation selects the applicable properties from the process object. This technique is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; 0..2 | % {start-process notepad -WindowStyle minimized}&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $n = Get-Process notepad&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;0..2 | % {&amp;quot;the name is &amp;quot; + $n[$_].name + &amp;quot; and the PID is &amp;quot; + $n[$_].id}&lt;/p&gt;
&lt;p&gt;Rather than using concatenation or a subexpression to display the values from the object/property, a cleaner (and therefore, more reliable) method uses parameter substitution and the format operator. At first glance, this technique appears strange&amp;mdash;although not necessarily any stranger than indexing into the array to retrieve the property values. When you become familiar with this technique, you may find that you like it better than using concatenation or a subexpression. The basic pattern is shown here.&lt;/p&gt;
&lt;table cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;Opening quotation mark&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;Parameter to replace&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;Format operator&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;Value to substitute for first parameter&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;Closing quotation mark&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;ldquo;&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;{0}&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;-f&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;zero&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &lt;span&gt;&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;This pattern with the associated output is shown in the following example.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; &amp;quot;The first number {0} marks the first pattern&amp;quot; -f &amp;quot;zero&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;The first number zero marks the first pattern&lt;/p&gt;
&lt;p&gt;The advantage of using parameter substitution is that it is easier to format the output exactly as required. Going back to the example of indexing into the array, parameter substitution replaces the concatenation previously used. The revised example is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; 0..2 | % {start-process notepad -WindowStyle minimized}&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; $n = Get-Process notepad&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; 0..2 | % {&amp;quot;The name is {0} and the PID is {1}&amp;quot; -f $n[$_].name, $n[$_].id}&lt;/p&gt;
&lt;p&gt;The following output illustrates the use of parameter substitution to retrieve the property values from the array and to format the 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/3835.wes_2D00_4_2D00_28_2D00_12_2D00_7.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/3835.wes_2D00_4_2D00_28_2D00_12_2D00_7.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Well, that is enough for now about formatting output. Join me tomorrow when I will continue the discussion by examining the use of various cmdlets.&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=3493508" width="1" height="1" alt="" /&gt;</description></item></channel></rss>