minhee
minhee

Reputation: 5818

PowerShell equivalent of “dirname $0” from bash

I've used dirname "$0" which is an idiom to determine the path of the running script in bash, e.g.:

pushd "$(dirname "$0")"
data_dir="$(dirname "$0")/data/"

What's PowerShell equivalent of the above idiom?

Upvotes: 3

Views: 1129

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174505

Since PowerShell version 3.0, the execution context provides 2 script-scoped automatic variables:

  • $PSCommandPath - the file system path to the executing script, eg. C:\path\to\script.ps1
  • $PSScriptRoot - the immediate parent folder of the script, eg. C:\path\to

So the equivalent of your last statement would be as follows in PowerShell:

$dataDir = Join-Path $PSScriptRoot data

Upvotes: 4

Related Questions