locked
Script directory changing RRS feed

  • Question

  • Let's say I have script A at location
    c:\scripts\set1\A.ps1
    -----------------------

    I can get the curretn location of script using below code.
    $path = (split-path $MyInvocation.MyCommand.Path) 

    So it stores c:\scripts\set1

    But If I run this script from another script like below.

    c:\scripts\set2\B.ps1
    ----------------------------
    &"c:\scripts\set1\A.ps1"

    The $path is taking as c:\scripts\set2
    The parent script location.

    because of this my code functionality is changing when I try to call it from another script.

    Any resolution for this?


    Thanks and Regards, Uday Kiran Reddy Project Engineer Technology Infrastructure Services, Wipro Technologies ____________________________________________ Mob: +91-900-061-2578 | Email: uday1kiran@live.in

    Monday, May 14, 2018 3:49 PM

Answers

  • Try the automatic variable $PSScriptRoot.

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by uday1kiran Tuesday, May 15, 2018 10:01 AM
    Monday, May 14, 2018 5:32 PM
  • Let's say I have script A at location
    c:\scripts\set1\A.ps1
    -----------------------

    I can get the curretn location of script using below code.
    $path = (split-path $MyInvocation.MyCommand.Path) 

    So it stores c:\scripts\set1

    But If I run this script from another script like below.

    c:\scripts\set2\B.ps1
    ----------------------------
    &"c:\scripts\set1\A.ps1"

    The $path is taking as c:\scripts\set2
    The parent script location.

    because of this my code functionality is changing when I try to call it from another script.

    Any resolution for this?


    Thanks and Regards, Uday Kiran Reddy Project Engineer Technology Infrastructure Services, Wipro Technologies ____________________________________________ Mob: +91-900-061-2578 | Email: uday1kiran@live.in


    $MyInvocation.PSScriptRoot and $MyInvocation.PSCommandPath contains information about the invoker or calling script.

    $PSScriptRoot and $PSCommandPath contain information about the current script.

    Regards.

    Octavio.

     
    • Marked as answer by uday1kiran Tuesday, May 15, 2018 10:01 AM
    Monday, May 14, 2018 5:59 PM

All replies

  • Working fine for me.

    I have script 1 F:\testing\test\a\test.ps1, this script only contains

    $path = (split-path $MyInvocation.MyCommand.Path)
    Write-Host $path

    Then in F:\Testing\Test I have script RunMe.ps1 which contains

    & "F:\Testing\Test\A\Test.ps1"
    Open powershell, cd to F:\Testing\Test and run Runme.ps1, and the output is F:\Testing\Test\A


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Monday, May 14, 2018 4:53 PM
  • Try the automatic variable $PSScriptRoot.

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by uday1kiran Tuesday, May 15, 2018 10:01 AM
    Monday, May 14, 2018 5:32 PM
  • Let's say I have script A at location
    c:\scripts\set1\A.ps1
    -----------------------

    I can get the curretn location of script using below code.
    $path = (split-path $MyInvocation.MyCommand.Path) 

    So it stores c:\scripts\set1

    But If I run this script from another script like below.

    c:\scripts\set2\B.ps1
    ----------------------------
    &"c:\scripts\set1\A.ps1"

    The $path is taking as c:\scripts\set2
    The parent script location.

    because of this my code functionality is changing when I try to call it from another script.

    Any resolution for this?


    Thanks and Regards, Uday Kiran Reddy Project Engineer Technology Infrastructure Services, Wipro Technologies ____________________________________________ Mob: +91-900-061-2578 | Email: uday1kiran@live.in


    $MyInvocation.PSScriptRoot and $MyInvocation.PSCommandPath contains information about the invoker or calling script.

    $PSScriptRoot and $PSCommandPath contain information about the current script.

    Regards.

    Octavio.

     
    • Marked as answer by uday1kiran Tuesday, May 15, 2018 10:01 AM
    Monday, May 14, 2018 5:59 PM
  • Also tested in my environment. Same output as Octavio. It's working fine without problems.

    Script A located in C:\Scripts\A\A.ps1

    A.ps1

    $path = (split-path $MyInvocation.MyCommand.Path)
    Write-Host $path
    
    & C:\Scripts\B\B.ps1

    Script B located in C:\Scripts\B\B.ps1

    B.ps1

    $path = (split-path $MyInvocation.MyCommand.Path)
    Write-Host $path

    When i run A.ps1, it calls script B.ps1 from another dir, see output:

    C:\Scripts\A
    C:\Scripts\B
    PS C:\Scripts\A>

    What's your powershell version ($PSVersionTable)? Can you share the script where this problem accurs?

    (not prefered) if needed you can use Push-Location, then call other script and then use Pop-Location to get back to the most recently location where Push-Location is used.


    Sincerely, Martien van Dijk. Please remember to mark the replies as answers if they help and unmark them if they provide no help. Check out My Blog!



    Monday, May 14, 2018 8:24 PM
  • I am just checking of the replys answers your question. Is there any update on this? If a specific reply match your answer please mark this reply as answer.

    Sincerely, Martien van Dijk. Please remember to mark the replies as answers if they help and unmark them if they provide no help. Check out My Blog!

    Tuesday, May 15, 2018 7:13 AM