Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6236171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:48:13+00:00 2026-05-24T10:48:13+00:00

I want to be able to tell what path my executing script was run

  • 0

I want to be able to tell what path my executing script was run from.
This will often not be $pwd.

I need to call other scripts that are in a folder structure relative to my script and while I could hard code the paths, that’s both distasteful and a bit of a pain in the neck when trying to promote from “dev” to “test” to “production”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:48:14+00:00Added an answer on May 24, 2026 at 10:48 am

    The ubiquitous script originally posted by Jeffrey Snover of the PowerShell team (given in Skyler’s answer) and the variations posted by Keith Cedirc, and EBGreen, all suffer from a serious drawback–whether the code reports what you expect depends on where you call it!

    My code below overcomes this problem by simply referencing script scope instead of parent scope:

    function Get-ScriptDirectory
    {
        Split-Path $script:MyInvocation.MyCommand.Path
    }
    

    To illustrate the problem, I created a test vehicle that evalutes the target expression in four different ways. (The bracketed terms are the keys to the following result table.)

    1. inline code [inline]
    2. inline function, i.e. function in the main program [inline function]
    3. Dot-sourced function, i.e. the same function moved to a separate .ps1 file [dot source]
    4. Module function, i.e. the same function moved to a separate .psm1 file [module]

    The last two columns show the result of using script scope (i.e. $script:) or with parent scope (with -scope 1). A result of “script” means that the invocation correctly reported the location of the script. The “module” result means the invocation reported the location of the module containing the function rather than the script that called the function; this indicates a drawback of both functions that you cannot put the function in a module.

    Setting the module issue aside the remarkable observation from the table is that using the parent scope approach fails most of the time (in fact, twice as often as it succeeds).

    table of input combinations

    Finally, here is the test vehicle:

    function DoubleNested()
    {
        "=== DOUBLE NESTED ==="
        NestCall
    }
    
    function NestCall()
    {
        "=== NESTED ==="
        "top level:"
        Split-Path $script:MyInvocation.MyCommand.Path
        #$foo = (Get-Variable MyInvocation -Scope 1).Value
        #Split-Path $foo.MyCommand.Path
        "immediate func call"
        Get-ScriptDirectory1
        "dot-source call"
        Get-ScriptDirectory2
        "module call"
        Get-ScriptDirectory3
    }
    
    function Get-ScriptDirectory1
    {
        Split-Path $script:MyInvocation.MyCommand.Path
        # $Invocation = (Get-Variable MyInvocation -Scope 1).Value
        # Split-Path $Invocation.MyCommand.Path
    }
    
    . .\ScriptDirFinder.ps1
    Import-Module ScriptDirFinder -force
    
    "top level:"
    Split-Path $script:MyInvocation.MyCommand.Path
    #$foo = (Get-Variable MyInvocation -Scope 1).Value
    #Split-Path $foo.MyCommand.Path
    
    "immediate func call"
    Get-ScriptDirectory1
    "dot-source call"
    Get-ScriptDirectory2
    "module call"
    Get-ScriptDirectory3
    
    NestCall
    DoubleNested
    

    Contents of ScriptDirFinder.ps1:

    function Get-ScriptDirectory2
    {
        Split-Path $script:MyInvocation.MyCommand.Path
    #   $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    #   Split-Path $Invocation.MyCommand.Path
    }
    

    Contents of ScriptDirFinder.psm1:

    function Get-ScriptDirectory3
    {
        Split-Path $script:MyInvocation.MyCommand.Path
    #   $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    #   Split-Path $Invocation.MyCommand.Path
    }
    

    I am not familiar with what was introduced in PowerShell 2, but it could very well be that script scope did not exist in PowerShell 1, at the time Jeffrey Snover published his example.

    I was surprised when, though I found his code example proliferated far and wide on the web, it failed immediately when I tried it! But that was because I used it differently than Snover’s example (I called it not at script-top but from inside another function (my “nested twice” example).)

    2011.09.12 Update

    You can read about this with other tips and tricks on modules in my just-published article on Simple-Talk.com:
    Further Down the Rabbit Hole: PowerShell Modules and Encapsulation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All I want to do is be able to tell whether or not a
I want to be able to make an HTTP call updating some select boxes
I want to be able to do this. MyInterface interface = new ServiceProxyHelper<ProxyType>(); Here's
I want to be able to generate PDF output from my (native) C++ Windows
I want to be able to tell if a view is visible in my
I want to deploy a piece of software to pcs that will need to
I want to use a temp directory that will be unique to this build.
I want to be able to tell if a any number in an int[]
Objective I want to be able to run the Applescript to open multiple applications
I want to be able to capture the exception that is thrown when a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.