Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call a file from the same folder that I am in (in script)

Tags:

I would like to call script B from script A without specifying the complete path. I've tried with

.\scriptb.ps1 

but that doesn't work. Do I really have to specify the entire path?

(sorry for this quite basic question, but google couldn't help me!)

like image 450
Sune Avatar asked Feb 22 '12 12:02

Sune


People also ask

How do I go back one directory in PowerShell?

2 or later you can do cd - to navigate to your previous directory. cd is the alias for Set-Location . Adding paramerter + or - goes forward or backward through your location history. +1 This is the best answer for modern users.

What is $PSScriptRoot in PowerShell?

$PSScriptRootContains the full path of the executing script's parent directory. In PowerShell 2.0, this variable is valid only in script modules ( . psm1 ). Beginning in PowerShell 3.0, it's valid in all scripts.

How do I run a script from another PS script?

You don't need Start-Process. PowerShell scripts can run other scripts. Just put the command that runs the second script as a command in the first script (the same way as you would type it on the PowerShell command line).


2 Answers

i use the powershell variable which has an easy name to remember what is it for.

$PSScriptRoot\scriptb.ps1 
like image 185
toshi Avatar answered Sep 21 '22 03:09

toshi


it is possible using $MyInvocation as follows:

$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent $scriptPath = Join-Path $executingScriptDirectory "scriptb.ps1" Invoke-Expression ".\$scriptPath"  
like image 44
Tomas Panik Avatar answered Sep 23 '22 03:09

Tomas Panik