<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Array Literals In PowerShell</title><link>http://blogs.msdn.com/powershell/archive/2007/01/23/array-literals-in-powershell.aspx</link><description>The first thing to understand is that there are no array literals in PowerShell J Arrays are built using operators or casts. The way to build an array in PowerShell is to use the comma operator as shown in the following examples: $a = , 1 # array of one</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Array Literals In PowerShell</title><link>http://blogs.msdn.com/powershell/archive/2007/01/23/array-literals-in-powershell.aspx#1517080</link><pubDate>Wed, 24 Jan 2007 02:04:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1517080</guid><dc:creator>Adrian Milliner</dc:creator><description>&lt;p&gt;I suppose the big question is how do you explain this in 2 lines in the PowerShell quickstart guide.&lt;/p&gt;</description></item><item><title>re: Array Literals In PowerShell</title><link>http://blogs.msdn.com/powershell/archive/2007/01/23/array-literals-in-powershell.aspx#1519736</link><pubDate>Wed, 24 Jan 2007 08:53:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1519736</guid><dc:creator>keith_hill</dc:creator><description>&lt;p&gt;OK, riddle me this. &amp;nbsp;I have a function that may return nothing, a scalar or a collection. &amp;nbsp;I will attempt to foreach the results of the function call so I would like to use the @() around the results in the function before returning them. &amp;nbsp;That way the foreach would see either A) an empty collection or B) a collection with one or more elements in it.&lt;/p&gt;
&lt;p&gt;Unfortunately it seems that PowerShell converts an empty array returned from a function to $null. &amp;nbsp;In this case the $null result causes the foreach to iterate and when I then try to use the loop variable in a .NET method call I get a NullReferenceException. &lt;/p&gt;
&lt;p&gt;So I essentially have to test for null results before performing the foreach loop. That's a bummer because it appeared as though the @() would get me out of having to do that. &amp;nbsp;:-(&lt;/p&gt;</description></item><item><title>re: Array Literals In PowerShell</title><link>http://blogs.msdn.com/powershell/archive/2007/01/23/array-literals-in-powershell.aspx#1521255</link><pubDate>Wed, 24 Jan 2007 14:45:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1521255</guid><dc:creator>James</dc:creator><description>&lt;p&gt;I realise this is a judgement call, but did you find that the need to do that was so frequent that it warranted the shorthand '@' over [array], or was this pre-emptive?&lt;/p&gt;
&lt;p&gt;Let me try to put that another way that might sound less like a challenge. Shells have traditionally gained a very baroque syntax as they evolved, and I've been very pleased with how clean the PowerShell language is by comparison. Is that down to concious effort to avoid unnecessary syntax (in which case, I'd welcome further justification of the @ syntax, particularly any empirical observations you made), or purely a function of its age?&lt;/p&gt;
&lt;p&gt;In general, I'd like to know more about how the language was designed, because it sets a better context for what it does.&lt;/p&gt;
&lt;p&gt;By the way, your book &amp;quot;PowerShell in Action&amp;quot; costs $29.69 to preorder on amazon.com, but &amp;nbsp;	&amp;#163;30.39 from amazon.co.uk: at US$60.02, it's twice the price for people in the UK. At that difference, I shan't be buying it. You should complain to your publisher.&lt;/p&gt;</description></item><item><title>re: Array Literals In PowerShell</title><link>http://blogs.msdn.com/powershell/archive/2007/01/23/array-literals-in-powershell.aspx#1521263</link><pubDate>Wed, 24 Jan 2007 14:52:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1521263</guid><dc:creator>PowerShellTeam</dc:creator><description>&lt;p&gt;Hmmm - Keith - this should work exactly as you described. Here's a function that returns an empty array:&lt;/p&gt;
&lt;p&gt;PS (48) &amp;gt; function empty { @() }&lt;/p&gt;
&lt;p&gt;Used in foreach, it executes exactly 0 times.&lt;/p&gt;
&lt;p&gt;PS (49) &amp;gt; foreach ($i in @(empty)) { &amp;quot;i is $i&amp;quot; }&lt;/p&gt;
&lt;p&gt;And here's the example with two statements, neither of which return anything.&lt;/p&gt;
&lt;p&gt;PS (50) &amp;gt; foreach ($i in @(write-host &amp;quot;Foo&amp;quot;; empty)) { &amp;quot;i is $i&amp;quot; }&lt;/p&gt;
&lt;p&gt;Foo&lt;/p&gt;
&lt;p&gt;An expression that returns nothing is only converted to null in a scalar context. In an array context, it is treated as an empty array.&lt;/p&gt;
&lt;p&gt;Can you provide an example where this fails? We may have a bug in that case.&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;
&lt;p&gt;-bruce&lt;/p&gt;
</description></item><item><title>re: Array Literals In PowerShell</title><link>http://blogs.msdn.com/powershell/archive/2007/01/23/array-literals-in-powershell.aspx#1531392</link><pubDate>Thu, 25 Jan 2007 23:11:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1531392</guid><dc:creator>PowerShellTeam</dc:creator><description>&lt;p&gt;Re: the amazon UK pricing - I've contacted the publisher and they're looking into it.&lt;/p&gt;
&lt;p&gt;Next - about @( ... ) - sequences in PowerShell were one of the toughest design areas. Traditional shells are approximately typeless and we needed to get that sort of behaviour with PowerShell with pipes. We experimented with a variety of approaches for having the system figure things out but in the end we found that it was most effective to give the control back to the user. The @() shortcut is valuable both because it provides a concise way of handling a common scenario and because it means that you don't have to explicitly work with types. (Explicit use of types are (or were) considered a more advanced user scenario - fine for users with a programming background but rather less friendly to non-programmers.)&lt;/p&gt;
&lt;p&gt;And finally – regarding the design approach to the PowerShell language - the observation that shell languages become baroque through accretion of &amp;nbsp;features &amp;nbsp;is an excellent one. This is something that was very much in the forefront of our minds in designing the language. We wanted a language that could support basic command line-style operations but still scale to address complex scripting without an explosion of syntax. &amp;nbsp;Our solution was the idea of bimodal parsing – expression mode and command mode. This is still more complex than languages like Python but vastly simpler than most shells which are collections of micro-syntaxes. &amp;nbsp;In a sense, &amp;nbsp;the &amp;nbsp;PowerShell syntax is to traditional shell syntax what Java is to C++ - similar but not backwards compatible, rationalized and simplified to the best extent possible.&lt;/p&gt;
&lt;p&gt;-bruce&lt;/p&gt;
</description></item><item><title>re: Array Literals In PowerShell</title><link>http://blogs.msdn.com/powershell/archive/2007/01/23/array-literals-in-powershell.aspx#1625561</link><pubDate>Thu, 08 Feb 2007 12:05:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1625561</guid><dc:creator>JensG</dc:creator><description>&lt;p&gt;Bruce,&lt;/p&gt;
&lt;p&gt;if I type&lt;/p&gt;
&lt;p&gt; &amp;nbsp;PS&amp;gt; $a = 1,2,3&lt;/p&gt;
&lt;p&gt; &amp;nbsp;PS&amp;gt; $b = @($a)&lt;/p&gt;
&lt;p&gt;then, according to your explanation, I should have two arrays with the same content. &lt;/p&gt;
&lt;p&gt; &amp;nbsp;PS&amp;gt; $a.gettype()&lt;/p&gt;
&lt;p&gt; &amp;nbsp;IsPublic IsSerial Name &amp;nbsp; &amp;nbsp; BaseType&lt;/p&gt;
&lt;p&gt; &amp;nbsp;-------- -------- ---- &amp;nbsp; &amp;nbsp; --------&lt;/p&gt;
&lt;p&gt; &amp;nbsp;True &amp;nbsp; &amp;nbsp; True &amp;nbsp; &amp;nbsp; Object[] System.Array&lt;/p&gt;
&lt;p&gt;ok so far, but&lt;/p&gt;
&lt;p&gt; &amp;nbsp;PS&amp;gt; $b.gettype()&lt;/p&gt;
&lt;p&gt; &amp;nbsp;IsPublic IsSerial Name &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BaseType&lt;/p&gt;
&lt;p&gt; &amp;nbsp;-------- -------- ---- &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;--------&lt;/p&gt;
&lt;p&gt; &amp;nbsp;False &amp;nbsp; &amp;nbsp;True &amp;nbsp; &amp;nbsp; SZArrayEnumerator System.Object&lt;/p&gt;
&lt;p&gt;and furthermore&lt;/p&gt;
&lt;p&gt; &amp;nbsp;PS&amp;gt; &amp;nbsp;&amp;quot;Shouldn't be &amp;quot; + $a.length + &amp;quot; = &amp;quot; + $b.length + &amp;quot;?&amp;quot;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Shouldn't be 3 = ?&lt;/p&gt;
&lt;p&gt;Where's my error?&lt;/p&gt;
&lt;p&gt;JensG&lt;/p&gt;</description></item><item><title>re: Array Literals In PowerShell</title><link>http://blogs.msdn.com/powershell/archive/2007/01/23/array-literals-in-powershell.aspx#1633927</link><pubDate>Fri, 09 Feb 2007 13:49:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1633927</guid><dc:creator>Damian Powell</dc:creator><description>&lt;p&gt;Here's something that frustrates me with PowerShell:&lt;/p&gt;
&lt;p&gt;PS&amp;gt; $a = @( &amp;quot;this is a single-element array&amp;quot;)&lt;/p&gt;
&lt;p&gt;PS&amp;gt; $a&lt;/p&gt;
&lt;p&gt;this is a single-element array&lt;/p&gt;
&lt;p&gt;PS&amp;gt; $a -is [array]&lt;/p&gt;
&lt;p&gt;True&lt;/p&gt;
&lt;p&gt;PS&amp;gt; $a | get-member&lt;/p&gt;
&lt;p&gt; &amp;nbsp; TypeName: System.String&lt;/p&gt;
&lt;p&gt;Name &amp;nbsp; &amp;nbsp;MemberType &amp;nbsp; Definition&lt;/p&gt;
&lt;p&gt;---- &amp;nbsp; &amp;nbsp;---------- &amp;nbsp; ----------&lt;/p&gt;
&lt;p&gt;Clone &amp;nbsp; Method &amp;nbsp; &amp;nbsp; &amp;nbsp; System.Object Clone()&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;Now I know that I should really be able to remember the methods on an Array after being a .NET programmer for so long but I don't! I have a lousy memory and would far prefer to call get-member than fire up the MSDN library again! Why is this inconsistent with [hashtable] which behaves as I would expect?&lt;/p&gt;</description></item><item><title>re: Array Literals In PowerShell</title><link>http://blogs.msdn.com/powershell/archive/2007/01/23/array-literals-in-powershell.aspx#3019887</link><pubDate>Fri, 01 Jun 2007 10:26:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3019887</guid><dc:creator>Dave Lee</dc:creator><description>&lt;p&gt;Hi Bruce,&lt;/p&gt;
&lt;p&gt;This is an interesting behaviour I've noticed that I'm not sure was the original intention:&lt;/p&gt;
&lt;p&gt;I have a scenario such as the following:&lt;/p&gt;
&lt;p&gt;$moo = @(something_returned)&lt;/p&gt;
&lt;p&gt;however I have noticed that if &amp;quot;something_returned&amp;quot; is $null, i.e. we have $moo = @($null) then when I do $moo.Count, it turns out to be a single element array containing a $null value. &amp;nbsp;Not what I was expecting, rather I'd expect $moo would also be null.&lt;/p&gt;
&lt;p&gt;Attempting variations of the above task, the following actaully produces what I expect, the variable $moo being equal to $null&lt;/p&gt;
&lt;p&gt;[array]$moo = $null&lt;/p&gt;
&lt;p&gt;$moo = [array]$null &lt;/p&gt;
&lt;p&gt;Is there something weird occuring here?&lt;/p&gt;
&lt;p&gt;The reason I'm casting the &amp;quot;something_returned&amp;quot; to arrays is related to the problem Keith described with foreach statements entering and executing once for $null ...&lt;/p&gt;
&lt;p&gt;i.e&lt;/p&gt;
&lt;p&gt;foreach ($item in $null)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; Write-Host &amp;quot;this shouldn't display but does&amp;quot;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;</description></item><item><title>re: Array Literals In PowerShell</title><link>http://blogs.msdn.com/powershell/archive/2007/01/23/array-literals-in-powershell.aspx#3069606</link><pubDate>Mon, 04 Jun 2007 02:17:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3069606</guid><dc:creator>PowerShellTeam</dc:creator><description>&lt;p&gt;An array can be null or it can be empty. These are obviously two different things. An array can also have the value null as an array element. The @( ) construction retrieves anything that is emitted into the pipeline and makes sure it's wrapped in an array. Emitting $null is still emitting something so the constructed array isn't empty:&lt;/p&gt;
&lt;p&gt;PS (25) &amp;gt; @($null).count&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;PS (26) &amp;gt; @($null,$null).count&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;PS (27) &amp;gt; @($null,$null,$null).count&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;In a pipeline, it's possible that the pipeline won't write anything into the pipeline:&lt;/p&gt;
&lt;p&gt;PS (33) &amp;gt; @(get-process csrss).count # writes one object&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;PS (34) &amp;gt; @(get-process nosuchprocess*).count # wrote nothing so the array is empty&lt;/p&gt;
&lt;p&gt;0&lt;/p&gt;
&lt;p&gt;Alternatively, think about this way: @() is approximately equivalent to&lt;/p&gt;
&lt;p&gt;PS (45) &amp;gt; $null | foreach {$al=new-object collections.arraylist} {[void] $al.add($_)} {$al.count}&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;PS (46) &amp;gt; $null,$null | foreach {$al=new-object collections.arraylist} {[void] $al.add($_)} {$al.count}&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;(except, of course, you return the array instead of the count...)&lt;/p&gt;
&lt;p&gt;-bruce&lt;/p&gt;
</description></item><item><title>re: Array Literals In PowerShell</title><link>http://blogs.msdn.com/powershell/archive/2007/01/23/array-literals-in-powershell.aspx#9436374</link><pubDate>Fri, 20 Feb 2009 21:07:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9436374</guid><dc:creator>Michael Lewis </dc:creator><description>&lt;p&gt;Where all this gets really unintuitive is in the following case:&lt;/p&gt;
&lt;p&gt;PS &amp;gt; @() | where-object ($false) | foreach-object { &amp;quot;found something&amp;quot; }&lt;/p&gt;
&lt;p&gt;...which obviously returns nothing. &lt;/p&gt;
&lt;p&gt;However, if you break the (empty) pipeline up, storing the result into a variable, when the variable (which contains a scaler null) gets used in a pipeline it gets turned into an array or one item. &lt;/p&gt;
&lt;p&gt;Thus...&lt;/p&gt;
&lt;p&gt;PS &amp;gt; $store = @() | where-object ($false)&lt;/p&gt;
&lt;p&gt;PS &amp;gt; $store | foreach-object { &amp;quot;found something!&amp;quot; }&lt;/p&gt;
&lt;p&gt;found something!&lt;/p&gt;
&lt;p&gt;The simplest solution to which is to force the variable to be an array, like so:&lt;/p&gt;
&lt;p&gt;PS &amp;gt; $store = @() | where-object ($false)&lt;/p&gt;
&lt;p&gt;PS &amp;gt; @($store) | foreach-object { &amp;quot;found something!&amp;quot; }&lt;/p&gt;
&lt;p&gt;.. which correctly returns nothing.&lt;/p&gt;</description></item><item><title>re: Array Literals In PowerShell</title><link>http://blogs.msdn.com/powershell/archive/2007/01/23/array-literals-in-powershell.aspx#9650155</link><pubDate>Fri, 29 May 2009 02:16:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9650155</guid><dc:creator>Nick</dc:creator><description>&lt;p&gt;I've been playing around with a script that loads an XML file using the [xml] handler, and I've found an interesting bit of behavior in the way that collections of subnodes are handled depending on whether or not you use XPaths or property (dot) notation. As this shows, property notation cannot always simply be used as a shortcut for selecting nodes.&lt;/p&gt;
&lt;p&gt;I've spent quite a while playing around with the behavior of foreach with scalars, collections and nulls and I think I see what the issue is, and I was wondering if you could confirm my thinking on this, and maybe consider a design tradeoff of changing the behavior.&lt;/p&gt;
&lt;p&gt;Let's say I have a document with a &amp;lt;Root&amp;gt; root node and two &amp;lt;Child&amp;gt; nodes under that root. Here's what happens:&lt;/p&gt;
&lt;p&gt;PS &amp;gt; $myDoc = ([xml](Get-Content $docPath))&lt;/p&gt;
&lt;p&gt;PS &amp;gt; @($myDoc.SelectNodes(&amp;quot;/Root/Child&amp;quot;)).Count&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;PS &amp;gt; @($myDoc.Root.Child).Count&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;Now edit the document so there are zero &amp;lt;Child&amp;gt; nodes, reload it into the $myDoc variable, and try again:&lt;/p&gt;
&lt;p&gt;PS &amp;gt; @($myDoc.SelectNodes(&amp;quot;/Root/Child&amp;quot;)).Count&lt;/p&gt;
&lt;p&gt;0&lt;/p&gt;
&lt;p&gt;PS &amp;gt; @($myDoc.Root.Child).Count&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;That &amp;quot;1&amp;quot; will cause a foreach loop to run (with a null value, natch), among other things. My guess is that SelectNodes explicitly returns an empty collection, which &amp;quot;transcends&amp;quot; the @ operator correctly and is represented as a true empty collection. $myDoc.Root.Child, being a dynamically generated property, returns $null instead of an empty collection, which when @'ed becomes a collection of size 1, which is what happens when splatting any null variable.&lt;/p&gt;
&lt;p&gt;Is this correct? Would it make more sense for those dynamically generated properties to return empty collections instead of $null?&lt;/p&gt;</description></item></channel></rss>