Include relative files in PowerShell

Dot-sourcing is the simplest option albeit not particularly pretty, as already stated. However, this format makes it just a tad bit cleaner:

$ScriptDirectory = Split-Path $MyInvocation.MyCommand.Path
. (Join-Path $ScriptDirectory ScriptA.ps1)

Also, a note about relative paths that is useful to make explicit: the original post might seem to imply wanting a path relative to the current working directory; in reality the intention is to be relative to the current script's source directory (as alex2k8's own code sample indicates). Thus, this allows the current script to access other scripts from the same repository.


You can utilize the $PSScriptRoot parameter like this:

. "$PSScriptRoot\script.ps1"

You can dot-source (include) the file:

. .\scriptA.ps1

To get the full path of the script:

Resolve-Path .\scriptA.ps1