To get the full path of the script we need to use the $myInvocation command. This is an automatic variable and it is only invoked when the script or the function is executed. $MyInvocation.
Using the $PSScriptRoot Variable to Get the Current Location of the PowerShell Script. If you are running PowerShell version 3 or later, an automatic variable has been introduced to store the current file or module's directory. The $PSScriptRoot variable returns the value of the directory of the running script.
By default it is located in C:\Users\<username>.
Use the Get-ChildItem cmdlet in PowerShell to get the full path of the file in the current directory. Get-ChildItem returns one or more items from the specified location and using the file FullName property, it gets the full path of the file.
For PowerShell 3.0
users - following works for both modules and script files:
function Get-ScriptDirectory {
Split-Path -parent $PSCommandPath
}
From Get-ScriptDirectory to the Rescue blog entry ...
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
Split-Path $MyInvocation.MyCommand.Path -Parent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With