<?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>Controlling the Scope of Variables</title><link>http://blogs.msdn.com/powershell/archive/2007/04/14/controlling-the-scope-of-variables.aspx</link><description>&amp;lt; Wizard Warning – this topic is for wizards. Non-wizards may get confused or hurt themselves.&amp;gt; PowerShell is …. well … powerful. I don't think most people have picked up on exactly how powerful it really is. There is nothing wrong with that, we</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Controlling the Scope of Variables</title><link>http://blogs.msdn.com/powershell/archive/2007/04/14/controlling-the-scope-of-variables.aspx#2136167</link><pubDate>Sat, 14 Apr 2007 23:55:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2136167</guid><dc:creator>netvance</dc:creator><description>&lt;p&gt;Maybe we should start our powershell software development a bit easier :) The Private statement in VBScript is concerned with classes; we’re going to take the liberty of extending the definition of Private here, primarily because we aren’t going to talk about classes. Instead of talking about private variables in terms of classes, we’re going to talk about private variables in terms of scope.&lt;/p&gt;
&lt;p&gt;In Windows PowerShell all you have to do is set the scope to Private. For example, this command creates a variable named $a with the scope set to private. Note that, when specifying a scope, you do not preface the variable name with a $; however, in your commands you will still refer to this variable as $a.&lt;/p&gt;
&lt;p&gt;Here’s the command:&lt;/p&gt;
&lt;p&gt;$Private:a = 5&lt;/p&gt;
&lt;p&gt;Because this is a private variable it will be available only in the scope in which it was created. If you type $a from the command prompt you’ll get back the value of $a. However, if you run a script that simply echoes back the value of $a you won’t get back anything; that’s because, as a private variable, $a is not available outside the command shell scope.&lt;/p&gt;</description></item><item><title>re: PS blogger</title><link>http://blogs.msdn.com/powershell/archive/2007/04/14/controlling-the-scope-of-variables.aspx#2136344</link><pubDate>Sun, 15 Apr 2007 00:16:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2136344</guid><dc:creator>james beard</dc:creator><description>&lt;p&gt; Dear PS,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Do you have an email address?&lt;/p&gt;</description></item><item><title>Interesting Finds: April 15, 2007</title><link>http://blogs.msdn.com/powershell/archive/2007/04/14/controlling-the-scope-of-variables.aspx#2145118</link><pubDate>Sun, 15 Apr 2007 17:44:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2145118</guid><dc:creator>Jason Haley</dc:creator><description /></item><item><title>re: Controlling the Scope of Variables</title><link>http://blogs.msdn.com/powershell/archive/2007/04/14/controlling-the-scope-of-variables.aspx#2193496</link><pubDate>Thu, 19 Apr 2007 18:50:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2193496</guid><dc:creator>Clift Norris</dc:creator><description>&lt;p&gt;First let me say that PowerShell has REALLY helped me and my company.&lt;/p&gt;
&lt;p&gt;We have used the tool to do some very interesting things!&lt;/p&gt;
&lt;p&gt;Kudos to the entire PSH development team.&lt;/p&gt;
&lt;p&gt;But the scoping rules described above are 'unexpected'.&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;quot;This is soooo powerful.&amp;quot;&lt;/p&gt;
&lt;p&gt;Yes, it is powerful, but it is also error-prone. &amp;nbsp;Along with&lt;/p&gt;
&lt;p&gt;&amp;quot;GOTO considered harmful&amp;quot; we have over the years learned that&lt;/p&gt;
&lt;p&gt;global variables are harmful if not used carefully and sparingly.&lt;/p&gt;
&lt;p&gt;Being able to control the scope of variables and avoid unintended&lt;/p&gt;
&lt;p&gt;access to variables is something that experienced programmers&lt;/p&gt;
&lt;p&gt;have come to rely upon.&lt;/p&gt;
&lt;p&gt;It could be that my concern is due to my use of PowerShell in a&lt;/p&gt;
&lt;p&gt;manner that is not entirely as the authors intended.&lt;/p&gt;
&lt;p&gt;I'm using it as a scripting language (on steroids!) and not so&lt;/p&gt;
&lt;p&gt;much as a &amp;quot;shell&amp;quot;. &amp;nbsp; Used as a shell, the above scoping concepts &lt;/p&gt;
&lt;p&gt;are surely powerful and helpful. &amp;nbsp;But when my scripts get to be &lt;/p&gt;
&lt;p&gt;a couple hundred lines of code, I begin to have reservations&lt;/p&gt;
&lt;p&gt;about the PowerShell scoping rules. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Yes, the scoping rules allow some very clever tasks to be accomplished.&lt;/p&gt;
&lt;p&gt;I suppose 'with power comes responsibility' and we just need&lt;/p&gt;
&lt;p&gt;to be diligent in our use of scoping prefixes ($global:foo).&lt;/p&gt;</description></item><item><title>Fun with Paths</title><link>http://blogs.msdn.com/powershell/archive/2007/04/14/controlling-the-scope-of-variables.aspx#2303643</link><pubDate>Sat, 28 Apr 2007 00:21:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2303643</guid><dc:creator>Windows PowerShell</dc:creator><description>&lt;p&gt;Jeffrey wrote a great post for wizards about the scope of variables in PowerShell, so to maintain balance,&lt;/p&gt;
</description></item><item><title>Line Count with PowerShell</title><link>http://blogs.msdn.com/powershell/archive/2007/04/14/controlling-the-scope-of-variables.aspx#5616013</link><pubDate>Tue, 23 Oct 2007 07:18:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5616013</guid><dc:creator>code-o-rama</dc:creator><description>&lt;p&gt;Starting to get the hang of PowerShell. Today I wanted to quickly count the number of lines in some source&lt;/p&gt;
</description></item><item><title>re: Controlling the Scope of Variables</title><link>http://blogs.msdn.com/powershell/archive/2007/04/14/controlling-the-scope-of-variables.aspx#8645282</link><pubDate>Tue, 24 Jun 2008 06:33:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8645282</guid><dc:creator>Tim Johnson</dc:creator><description>&lt;p&gt;First off, excellent article. &amp;nbsp;It's exactly the answer to what I was wondering about scoping in PowerShell.&lt;/p&gt;
&lt;p&gt;Clift, if you're still following this, what is your concern, specifically? &amp;nbsp;All variables are local in scope by default, so it's not like all variables are being made global, and anyone who is familiar with a scripting language should be familiar with the concept of having variables inherit down the scope. &amp;nbsp;I don't think anyone will be accidentally setting up-scoped variables, given the default syntax, so am I missing something (entirely possible)?&lt;/p&gt;</description></item><item><title>re: Controlling the Scope of Variables</title><link>http://blogs.msdn.com/powershell/archive/2007/04/14/controlling-the-scope-of-variables.aspx#9735892</link><pubDate>Fri, 12 Jun 2009 19:33:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9735892</guid><dc:creator>Doug Clutter</dc:creator><description>&lt;p&gt;Have only been working with PowerShell a short, short time, but I've found it incredibly useful.&lt;/p&gt;
&lt;p&gt;How can we control the scope of functions? &amp;nbsp;For instance, lets say I have this file:&lt;/p&gt;
&lt;p&gt;SomeUsefulFunction.ps1&lt;/p&gt;
&lt;p&gt;--------------&lt;/p&gt;
&lt;p&gt;function SomeUsefulFunction { ... }&lt;/p&gt;
&lt;p&gt;Now, I want to use the function, so I try this:&lt;/p&gt;
&lt;p&gt;PS&amp;gt; ./SomeUsefulFunction.ps1&lt;/p&gt;
&lt;p&gt;PS&amp;gt; SomeUsefulFunction &amp;quot;parm1&amp;quot;&lt;/p&gt;
&lt;p&gt;The term 'SomeUsefulFunction' is not recognized...&lt;/p&gt;
&lt;p&gt;Taking a cue from this post, I tried this:&lt;/p&gt;
&lt;p&gt;function 1:SomeUsefulFunction { ... }&lt;/p&gt;
&lt;p&gt;...but that didn't work.&lt;/p&gt;
&lt;p&gt;Perhaps, it would be best to address the underlying issue? &amp;nbsp;What are some of the &amp;quot;best practices&amp;quot; to organizing PS code? &amp;nbsp;Should we even be writing functions, or should we structure things like this:&lt;/p&gt;
&lt;p&gt;SomeUsefulFunction.ps1&lt;/p&gt;
&lt;p&gt;--------------&lt;/p&gt;
&lt;p&gt;params ( ... )&lt;/p&gt;
&lt;p&gt;process { ... }&lt;/p&gt;
&lt;p&gt;end { ... }&lt;/p&gt;
&lt;p&gt;PS&amp;gt; ./SomeUsefulFunction.ps1 &amp;quot;parm1&amp;quot;&lt;/p&gt;</description></item></channel></rss>