{"id":10061,"date":"2006-07-06T01:59:00","date_gmt":"2006-07-06T01:59:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/07\/06\/how-can-a-script-tell-what-directory-it-was-run-from\/"},"modified":"2019-02-18T13:21:34","modified_gmt":"2019-02-18T20:21:34","slug":"how-can-a-script-tell-what-directory-it-was-run-from","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/how-can-a-script-tell-what-directory-it-was-run-from\/","title":{"rendered":"How can a script tell what directory it was run from?"},"content":{"rendered":"<p>PSMDTAG:FAQ: How can a script determine what directory it was invoked from?<br \/>PSMDTAG:FAQ: What is $MyInvocation?<br \/>PSMDTAG:FAQ: Why is $MyInvocation.ScriptName empty?<\/p>\n<p>Create 2 scripts (First.PS1 and Second.PS1) to explore what is going on with $MyInvocation and how you can use it to determine what directory a script was invoked from:<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; <strong><font color=\"#000080\" size=\"2\">cat first.ps1<br \/><\/font><\/strong>&#8220;***** FIRST MYINVOCATION *****&#8221;<br \/>$myInvocation |Format-List *<br \/>.\\second.ps1<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt;<strong><font color=\"#000080\"> <font size=\"2\">cat second.ps1<br \/><\/font><\/font><\/strong>&#8220;***** Second MYINVOCATION *****&#8221;<br \/>$myInvocation |Format-List *<br \/>&#8220;***** Second MYINVOCATION.MyCommand *****&#8221;<br \/>$myInvocation.MyCommand |Format-List *<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; <strong><font color=\"#000080\" size=\"2\">.\\first.ps1<br \/><\/font><\/strong>***** FIRST&nbsp;MYINVOCATION *****<\/font><br \/><font face=\"Courier New\" size=\"1\">MyCommand&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : first.ps1<br \/>ScriptLineNumber : 1<br \/>OffsetInLine&nbsp;&nbsp;&nbsp;&nbsp; : -2147483648<br \/><strong><font color=\"#ff0000\" size=\"2\">ScriptName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<br \/><\/font><\/strong>Line&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : .\\first.ps1<br \/>PositionMessage&nbsp; :<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; At line:1 char:11<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + .\\first.ps1 &lt;&lt;&lt;&lt;<br \/>InvocationName&nbsp;&nbsp; : .\\first.ps1<br \/>PipelineLength&nbsp;&nbsp; : 1<br \/>PipelinePosition : 1<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\"><\/font>&nbsp;<\/p>\n<p><font face=\"Courier New\" size=\"1\">***** Second MYINVOCATION *****<\/font><br \/><font face=\"Courier New\" size=\"1\">MyCommand&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : second.ps1<br \/>ScriptLineNumber : 3<br \/>OffsetInLine&nbsp;&nbsp;&nbsp;&nbsp; : 13<br \/><font size=\"2\"><strong><font color=\"#ff0000\">ScriptName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : C:\\ps\\first.ps1<\/font><\/strong><br \/><\/font>Line&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : .\\second.ps1<br \/>PositionMessage&nbsp; :<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; At C:\\ps\\first.ps1:3 char:13<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + .\\second.ps1 &lt;&lt;&lt;&lt;<br \/>InvocationName&nbsp;&nbsp; : .\\second.ps1<br \/>PipelineLength&nbsp;&nbsp; : 1<br \/>PipelinePosition : 1<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\"><\/font>&nbsp;<\/p>\n<p><font face=\"Courier New\" size=\"1\">***** Second MYINVOCATION.MyCommand *****<\/font><br \/><font face=\"Courier New\" size=\"1\"><strong><font color=\"#ff0000\" size=\"2\">Path&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : C:\\ps\\second.ps1<br \/><\/font><\/strong>Definition&nbsp; : C:\\ps\\second.ps1<br \/>Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : second.ps1<br \/>CommandType : ExternalScript<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt;<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">When you run First.ps1, you see that ScriptName is empty.&nbsp; The reason for this is that ScriptName refers to the name of the script that called you (Yes &#8211; we probably should have chosen a better name).&nbsp; Notice that when First.Ps1 calls Second.Ps1, ScriptName is the path to First.PS1.<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">Now look at $MyInvocation.MyCommand.&nbsp; The PATH property tells you the full path of the current script.&nbsp; If you want the directory you do the following:<\/font><\/p>\n<p><font face=\"Courier New\" color=\"#000080\" size=\"1\"><strong>$myDir = Split-Path -Parent $MyInvocation.MyCommand.Path<\/strong><br \/><\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">Jeffrey Snover [MSFT]<br \/>Windows PowerShell Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <\/font><a href=\"http:\/\/blogs.msdn.com\/PowerShell\"><font face=\"Courier New\" size=\"1\">http:\/\/blogs.msdn.com\/PowerShell<\/font><\/a><br \/><font face=\"Courier New\" size=\"1\">Visit the Windows PowerShell ScriptCenter at:&nbsp; <\/font><a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\"><font face=\"Courier New\" size=\"1\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/font><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>PSMDTAG:CMDLET:UTILITY: Split-Path<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PSMDTAG:FAQ: How can a script determine what directory it was invoked from?PSMDTAG:FAQ: What is $MyInvocation?PSMDTAG:FAQ: Why is $MyInvocation.ScriptName empty? Create 2 scripts (First.PS1 and Second.PS1) to explore what is going on with $MyInvocation and how you can use it to determine what directory a script was invoked from: PS&gt; cat first.ps1&#8220;***** FIRST MYINVOCATION *****&#8221;$myInvocation |Format-List [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[17,10],"class_list":["post-10061","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-cmdletutility","tag-faq"],"acf":[],"blog_post_summary":"<p>PSMDTAG:FAQ: How can a script determine what directory it was invoked from?PSMDTAG:FAQ: What is $MyInvocation?PSMDTAG:FAQ: Why is $MyInvocation.ScriptName empty? Create 2 scripts (First.PS1 and Second.PS1) to explore what is going on with $MyInvocation and how you can use it to determine what directory a script was invoked from: PS&gt; cat first.ps1&#8220;***** FIRST MYINVOCATION *****&#8221;$myInvocation |Format-List [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=10061"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10061\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=10061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}