How to run a powershell script from any path using PSScriptRoot concept

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

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
3 weeks, 5 days ago.

  • 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: 1041
    Points: 3,439
    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 ?

You must be logged in to reply to this topic.