Welcome Forums General PowerShell Q&A How to run a powershell script from any path using PSScriptRoot concept

Viewing 1 reply thread
  • Author
    Posts
    • #172802
      Participant
      Topics: 4
      Replies: 9
      Points: 28
      Rank: Member

      Hi ,

      I want to execute a powershell script from any path from local .

      Here   “src\Customization\Source”  is the fixed path

      function bat_file
      
                  $path =  "C:\Programs\data\Git\tc-pdm\src\Customization\Source" 
      }
      bat_file

      For running the abc.ps1 file we need to provide the whole path     ” cd  C:\Programs\data” and then “.\abc.ps1 ” in the powershell console

      But now i should be able to run the abc.ps1 file even if the path is different  “cd C:\Programs\temp1”. 

      I was thinking if we can use PSScriptRoot concept for this .

    • #172805
      Senior Moderator
      Topics: 8
      Replies: 1158
      Points: 4,045
      Helping Hand
      Rank: Community Hero

      $PSScriptRoot will have the path where the current executing script resides. If the script is in the same location of the caller script, you can use it.
      I would suggest to parameterize it giving a default value.

      • #172808
        Participant
        Topics: 4
        Replies: 9
        Points: 28
        Rank: Member

        Thanks for the response Prasoon .

        Do you have any idea about how to modify the above script using PSScriptRoot ?

Viewing 1 reply thread
  • The topic ‘How to run a powershell script from any path using PSScriptRoot concept’ is closed to new replies.