<?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>Get-ScriptDirectory to the Rescue   </title><link>http://blogs.msdn.com/powershell/archive/2007/06/19/get-scriptdirectory.aspx</link><description>The other day I was writing a script and decided that I wanted to break it into a couple of files and have the main script dot-source a library script in the same directory. Here is the problem that I ran into: PS&amp;gt; Get-ChildItem Directory: Microsoft.PowerShell.Core\FileSystem::C:\Temp\test</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Get-ScriptDirectory to the Rescue   </title><link>http://blogs.msdn.com/powershell/archive/2007/06/19/get-scriptdirectory.aspx#3408051</link><pubDate>Tue, 19 Jun 2007 19:31:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3408051</guid><dc:creator>jvierra</dc:creator><description>&lt;p&gt;So being a team member doesn't make one know everything. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;I put that in my startup script a long time ago. &amp;nbsp;Never thought that it needed to be noted. &amp;nbsp;Your blog made me realize that many things in PS are not clearly documented (It took me a bit of time to discover MyInvocation.Path.)&lt;/p&gt;
&lt;p&gt;It just proves, once again, that everything is there. &amp;nbsp;It just tazkes a bit of time and thought to uncover how to do it.&lt;/p&gt;
&lt;p&gt;Hurray for PowerShell! &amp;nbsp;Hooray for .NET!&lt;/p&gt;
&lt;p&gt;Great stuff Jeffrey / PS Team.&lt;/p&gt;</description></item><item><title>re: Get-ScriptDirectory to the Rescue   </title><link>http://blogs.msdn.com/powershell/archive/2007/06/19/get-scriptdirectory.aspx#3408965</link><pubDate>Tue, 19 Jun 2007 20:51:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3408965</guid><dc:creator>aharden</dc:creator><description>&lt;p&gt;Discussed in the newsgroup a few weeks ago: &lt;a rel="nofollow" target="_new" href="http://groups.google.com/group/microsoft.public.windows.powershell/browse_thread/thread/e57016f37273b1cb/6de1249b8b86936c#6de1249b8b86936c"&gt;http://groups.google.com/group/microsoft.public.windows.powershell/browse_thread/thread/e57016f37273b1cb/6de1249b8b86936c#6de1249b8b86936c&lt;/a&gt;&lt;/p&gt;</description></item><item><title>re: Get-ScriptDirectory to the Rescue   </title><link>http://blogs.msdn.com/powershell/archive/2007/06/19/get-scriptdirectory.aspx#3409130</link><pubDate>Tue, 19 Jun 2007 21:02:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3409130</guid><dc:creator>PowerShellTeam</dc:creator><description>&lt;p&gt;&amp;gt; Discussed in the newsgroup a few weeks ago:&lt;/p&gt;
&lt;p&gt;Doh!!!!&lt;/p&gt;
&lt;p&gt;Well I guess it proves this point: &lt;/p&gt;
&lt;p&gt;... you can always just post a question to our newsgroup Microsoft.Public.Windows.PowerShell and the community will help. &lt;/p&gt;
&lt;p&gt;Jeffrey Snover [MSFT]&lt;/p&gt;
&lt;p&gt;Windows Management Partner Architect&lt;/p&gt;
&lt;p&gt;Visit the Windows PowerShell Team blog at: &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/PowerShell"&gt;http://blogs.msdn.com/PowerShell&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Visit the Windows PowerShell ScriptCenter at: &lt;a rel="nofollow" target="_new" href="http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx"&gt;http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx&lt;/a&gt; &lt;/p&gt;
</description></item><item><title>re: Get-ScriptDirectory to the Rescue   </title><link>http://blogs.msdn.com/powershell/archive/2007/06/19/get-scriptdirectory.aspx#3416554</link><pubDate>Wed, 20 Jun 2007 06:49:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3416554</guid><dc:creator>Keith Hill</dc:creator><description>&lt;p&gt;It is very nice to know the trick about using the -scope on get-variable. &amp;nbsp;Thanks for pointing that out. &amp;nbsp;What we tend to do in our scripts (and we use a number of dot sourced PowerShell libraries) is this:&lt;/p&gt;
&lt;p&gt;$ScriptDir = split-path -parent $MyInvocation.MyCommand.Path&lt;/p&gt;
&lt;p&gt;. &amp;quot;$ScriptDir\TestUtils.ps1&amp;quot;&lt;/p&gt;
&lt;p&gt;at the top of each script.&lt;/p&gt;</description></item><item><title>re: Get-ScriptDirectory to the Rescue   </title><link>http://blogs.msdn.com/powershell/archive/2007/06/19/get-scriptdirectory.aspx#3419774</link><pubDate>Wed, 20 Jun 2007 11:33:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3419774</guid><dc:creator>Philippe Ditroia</dc:creator><description>&lt;p&gt;Another simple solution could be:&lt;/p&gt;
&lt;p&gt;push-location myScriptPath&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;pop-location&lt;/p&gt;</description></item><item><title>re: Get-ScriptDirectory to the Rescue   </title><link>http://blogs.msdn.com/powershell/archive/2007/06/19/get-scriptdirectory.aspx#3709735</link><pubDate>Thu, 05 Jul 2007 20:41:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3709735</guid><dc:creator>Sneal</dc:creator><description>&lt;p&gt;Thanks! &amp;nbsp;That tip saved the day for me. &amp;nbsp;I needed to run a powershell script from a UNC path which was loading an assembly from the same UNC path as the script. &amp;nbsp;It worked flawlessly!&lt;/p&gt;</description></item><item><title>Re: sourcing a file in a script</title><link>http://blogs.msdn.com/powershell/archive/2007/06/19/get-scriptdirectory.aspx#4674960</link><pubDate>Fri, 31 Aug 2007 23:03:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4674960</guid><dc:creator>Latest Newsgroup Posts</dc:creator><description>&lt;p&gt;You need to &amp;amp;quot;Dot source&amp;amp;quot; the file. Basically, its &amp;amp;quot;. &amp;amp;lt;filename&amp;amp;gt;&amp;amp;quot;. Check &amp;amp;quot;Get-ScriptDirectory&lt;/p&gt;
</description></item><item><title>re: Get-ScriptDirectory to the Rescue   </title><link>http://blogs.msdn.com/powershell/archive/2007/06/19/get-scriptdirectory.aspx#5412988</link><pubDate>Fri, 12 Oct 2007 06:12:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5412988</guid><dc:creator>Hristo Deshev</dc:creator><description>&lt;p&gt;Going to the parent scope for the script-level $MyInvocation variable does not always work. I just had this situation:&lt;/p&gt;
&lt;p&gt;function Test()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;$inv = (Get-Variable MyInvocation -Scope 1).Value&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;$inv.MyCommand | Format-List *&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;function Test2()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Test&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Test2&lt;/p&gt;
&lt;p&gt;Where the parent scope is actually the Test2 function not the script scope. You can always get the script scope $MyInvocation variable by using the $script namespace prefix:&lt;/p&gt;
&lt;p&gt;$scriptInvocation = $script:MyInvocation&lt;/p&gt;
&lt;p&gt;This works when called from any function scope no matter how deeply nested.&lt;/p&gt;</description></item><item><title>I had the same problem as Hristo</title><link>http://blogs.msdn.com/powershell/archive/2007/06/19/get-scriptdirectory.aspx#9283552</link><pubDate>Tue, 06 Jan 2009 00:50:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9283552</guid><dc:creator>Gordon Martin</dc:creator><description>&lt;p&gt;Let me start by saying I really, really &amp;nbsp;appreciate the information in this post - but I did run into the same problem as Hristo.&lt;/p&gt;
&lt;p&gt;I think it was a disservice to make a function for this trick. &amp;nbsp;A function suggests it can be referenced from numerous places, but clearly it will only work if called from the main script. &amp;nbsp;Also, I don't see much point in calling this repeatedly - the answer doesn't change no matter how often you call it. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;I think it makes much more sense to just put these two lines at the beginning of a script and leave the variable available for the duration of its execution:&lt;/p&gt;
&lt;p&gt;$Invocation = (Get-Variable MyInvocation -Scope 0).Value&lt;/p&gt;
&lt;p&gt;$ScriptPath = Split-Path $Invocation.MyCommand.Path&lt;/p&gt;
&lt;p&gt;Then later on you just make reference to it like so:&lt;/p&gt;
&lt;p&gt;$ReportPath = Join-Path $ScriptPath $ReportFile&lt;/p&gt;
&lt;p&gt;Not only is this much simpler, it will also work reliably from anywhere within the program.&lt;/p&gt;</description></item></channel></rss>