Include relative files in PowerShell

alex2k8 picture alex2k8 · Mar 26, 2009 · Viewed 65.7k times · Source

I would like to include script files with such pseudo syntax:

Include '.\scripA.ps1'

But the only thing I have found is some thing like this:

$thisScript = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
. ($thisScript + '.\scriptA.ps1')

that is ugly.

Is there some nice way to include scripts with relative paths?

Answer

chrischu picture chrischu · Nov 26, 2013

You can utilize the $PSScriptRoot parameter like this:

. "$PSScriptRoot\script.ps1"