Windows Vista Forums

script location?
  1. #1


    Justin Rich Guest

    script location?

    i want to find where my script is located... so when i run a command like...

    c:\>powershell c:\myscripts\thescript.ps1

    i'd get back either c:\myscripts or c:\myscripts\thescript.ps1

    i found this
    $myinvocation.mycommand.path but that returns the current location (in this
    case, c:\)
    as does $(pwd).path

    any other suggestions?

    Thanks
    Justin






      My System SpecsSystem Spec

  2. #2


    Marco Shaw [MVP] Guest

    Re: script location?

    Justin Rich wrote:

    > i want to find where my script is located... so when i run a command like...
    >
    > c:\>powershell c:\myscripts\thescript.ps1
    >
    > i'd get back either c:\myscripts or c:\myscripts\thescript.ps1
    >
    > i found this
    > $myinvocation.mycommand.path but that returns the current location (in this
    > case, c:\)
    > as does $(pwd).path
    >
    > any other suggestions?
    $myinvocation.invocationname

      My System SpecsSystem Spec

  3. #3


    Bob Landau Guest

    RE: script location?

    Does anyone know whether its possible mark a post as a "sticky"? It would be
    really nice to start a FAQ.

    Justin the MyInvocation variable is what you want. The state of this
    variable depends on the context in which its called which makes it somewhat
    confusing.

    Run this script it should help explain as you can see the output is
    different depending on whether you are at script of function scope.

    function test
    {
    echo "Entering function Test"
    echo "`$MyInvocation.InvocationName is $($MyInvocation.InvocationName)"
    echo "`$MyInvocation.MyCommand.name is $($MyInvocation.MyCommand.name)"
    echo "`$MyInvocation.ScriptName is $($MyInvocation.ScriptName)"
    echo "`$MyInvocation.MyCommand.path is $($MyInvocation.MyCommand.path)"
    echo "`$pwd.path $($pwd.path)"
    echo "Leaving function Test"
    }

    echo "Entering MyInvocation Test Scriptblock"

    echo "`$MyInvocation.InvocationName is $($MyInvocation.InvocationName)"
    echo "`$MyInvocation.MyCommand.name is $($MyInvocation.MyCommand.name)"
    echo "`$MyInvocation.ScriptName is $($MyInvocation.ScriptName)"
    echo "`$MyInvocation.MyCommand.path is $($MyInvocation.MyCommand.path)"
    echo "`$pwd.path $($pwd.path)"

    echo "Calling Test"
    test
    echo "Leaving MyInvocation Test Scriptblock"


    "Justin Rich" wrote:

    > i want to find where my script is located... so when i run a command like...
    >
    > c:\>powershell c:\myscripts\thescript.ps1
    >
    > i'd get back either c:\myscripts or c:\myscripts\thescript.ps1
    >
    > i found this
    > $myinvocation.mycommand.path but that returns the current location (in this
    > case, c:\)
    > as does $(pwd).path
    >
    > any other suggestions?
    >
    > Thanks
    > Justin
    >
    >
    >
    >

      My System SpecsSystem Spec

  4. #4


    Alex K. Angelopoulos Guest

    Re: script location?

    "Bob Landau" <BobLandau@xxxxxx> wrote in message
    news:F98C6B35-DDD7-4500-BEF9-417959A614BA@xxxxxx

    > Does anyone know whether its possible mark a post as a "sticky"? It would
    > be
    > really nice to start a FAQ.
    That would be nice; even though it wouldn't show for NNTP users like myself,
    it would be very useful to web uses.

    Alternatives (that help with bumping or finding info):

    I seem to recall that you can vote on posts in the web interface - if you
    can find that, a few positive votes may help.

    Personally, when looking for posts on a particular topic I usually just hit
    the Google Groups advanced search page and scan from there using keywords:

    http://groups.google.com/advanced_search

    Not FAQish, but it usually shows me everything relevant. Sadly, that's not
    useful to people who are new, since part of the problem is learning what
    you're really searching for.


      My System SpecsSystem Spec

  5. #5


    Alex K. Angelopoulos Guest

    Re: script location?

    Yet another alternative that will at least show you what members
    MyInvocation and MyInvocation.PSBase have is the following - it can be used
    in a test script file directly as well:

    function Demo-FunctionMyInvocation{
    Write-Output "`n`n+ MyInvocation members:"
    $MyInvocation | gm
    Write-Output "`n`n+ MyInvocation PSBase members:"
    $MyInvocation.PSBase | gm
    }

    Demo-FunctionMyInvocation

    Bob Landau's version is good for real browsing within a specific script; you
    can't just do something like
    $MyInvocation | fl *

    since some of the elements have predefined formatting that chokes with the
    format-list in place.

    "Justin Rich" <jrich523@xxxxxx> wrote in message
    news:uovLN0PtIHA.1240@xxxxxx

    > i want to find where my script is located... so when i run a command
    > like...
    >
    > c:\>powershell c:\myscripts\thescript.ps1
    >
    > i'd get back either c:\myscripts or c:\myscripts\thescript.ps1
    >
    > i found this
    > $myinvocation.mycommand.path but that returns the current location (in
    > this case, c:\)
    > as does $(pwd).path
    >
    > any other suggestions?
    >
    > Thanks
    > Justin
    >
    >
    >

      My System SpecsSystem Spec

  6. #6


    Justin Rich Guest

    Re: script location?

    Yeah sorry about the basic post, I was able to find the solution it was just
    doing weird things at fist, aka, not working... I tried a simple script with
    it and it kept tossing back the run from location... and after I got the
    post and tried $myinvocation.invocationname the $myinvocation.mycommand.path
    started to work...

    also I at first I was a little stumped because I would run the script from a
    command prompt and give it the no exit flag.. I had assumed that any objects
    containing this info would still have it... which apparently isn't the
    case..

    someone needs to make a really good FAQ site.. personally I don't find
    blogs to be the best solution, not always the easiest way to find the info
    moving forward..

    either way, I appreciate the help.

    Thanks
    Justin

    "Alex K. Angelopoulos" <aka(at)mvps.org> wrote in message
    news:%23EPY%23QctIHA.4560@xxxxxx

    > "Bob Landau" <BobLandau@xxxxxx> wrote in message
    > news:F98C6B35-DDD7-4500-BEF9-417959A614BA@xxxxxx

    >> Does anyone know whether its possible mark a post as a "sticky"? It would
    >> be
    >> really nice to start a FAQ.
    >
    > That would be nice; even though it wouldn't show for NNTP users like
    > myself, it would be very useful to web uses.
    >
    > Alternatives (that help with bumping or finding info):
    >
    > I seem to recall that you can vote on posts in the web interface - if you
    > can find that, a few positive votes may help.
    >
    > Personally, when looking for posts on a particular topic I usually just
    > hit the Google Groups advanced search page and scan from there using
    > keywords:
    >
    > http://groups.google.com/advanced_search
    >
    > Not FAQish, but it usually shows me everything relevant. Sadly, that's not
    > useful to people who are new, since part of the problem is learning what
    > you're really searching for.


      My System SpecsSystem Spec

script location? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Must I move the AD Groups I've created to the Default location so my script can see them, or is there another way...? Kelvin VB Script 1 20 Aug 2009
BUG? Invalid location after Set-Location \\MyMachine Roman Kuzmin PowerShell 3 01 Apr 2008
Copying files from the same location as the script greatbarrier86 PowerShell 5 13 Jan 2008
how to determine a script location? Jeff Jarrell PowerShell 6 01 Jun 2007
script location and $pwd Doug PowerShell 4 11 Dec 2006