How do I get the directory of the PowerShell script I execute?

Powershell

Powershell Problem Overview


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

How to do this?

Powershell Solutions


Solution 1 - Powershell

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2131116View Question on Stackoverflow
Solution 1 - PowershellAaron JensenView Answer on Stackoverflow