Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the directory of the PowerShell script I execute? [duplicate]

Tags:

powershell

I run a PowerShell script. How do I get the directory path of this script I run?

How to do this?

like image 237
user2131116 Avatar asked Jul 04 '13 03:07

user2131116


People also ask

How do I get the directory of a PowerShell script?

PowerShell Get Current Directory of Script File To get current directory of script file or running script, use $PSScriptRoot automatic variable.

How do I find my default PowerShell path?

On Windows, the location of the user-specific CurrentUser scope is the $HOME\Documents\PowerShell\Modules folder. The location of the AllUsers scope is $env:ProgramFiles\PowerShell\Modules . On non-Windows systems, the location of the user-specific CurrentUser scope is the $HOME/. local/share/powershell/Modules folder.

How do I go back to the original 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.


1 Answers

PowerShell 3 has the $PSScriptRoot automatic variable:

Contains the directory from which a script is being run.

In Windows PowerShell 2.0, this variable is valid only in script modules (.psm1). Beginning in Windows PowerShell 3.0, it is valid in all scripts.

Don't be fooled by the poor wording. PSScriptRoot is the directory of the current file.

In PowerShell 2, you can calculate the value of $PSScriptRoot yourself:

# PowerShell v2 $PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition 
like image 180
Aaron Jensen Avatar answered Oct 17 '22 09:10

Aaron Jensen