title: What's the best way to determine the location of the current PowerShell script?
body: <p>Whenever I need to reference a common module or script, I like to use paths relative to the current script file. That way, my script can always find other scripts in the library.</p>
  
  <p>So, what is the best, standard way of determining the directory of the current script? Currently, I'm doing:</p>
  
  <pre><code>$MyDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
  </code></pre>
  
  <p>I know in modules (.psm1) you can use <code>$PSScriptRoot</code> to get this information, but that doesn't get set in regular scripts (i.e. .ps1 files).</p>
  
  <p><strong>What's the canonical way to get the current PowerShell script file's location?</strong></p>