PowerShell script base directory

It is useful to be able to remove absolute paths from scripts, letting them run wherever they are located and using only relative paths for any files required during the script execution.
In order to achieve this the $PSScriptRoot automatic variable can be used; it only works within a script (its value is $null otherwise) and it contains the fully qualified filesystem path of the directory where the script is.
Retrieve the value of the variable at the beginning and then use it to build full paths adding the relative path to it:

$ScriptBaseDir = $PSScriptRoot

$InputFile    = $ScriptBaseDir\input.csv
$OutputFile = $ScriptBaseDir\output.csv


Comments

Popular posts from this blog

Decoding OCSP GET requests

Signing a CSR with an Enrollment Agent certificate

Compacting an AD CS database