When developing code/debugging and running just a section, the built in Get-ScriptDirectory throws an error as the the values it is looking for do not exist, this will return the current working directory that the PowerShell Session Console is in.
Code: Select all
#Sample function that provides the location of the script
function Get-ScriptDirectory
{
<#
.SYNOPSIS
Get-ScriptDirectory returns the proper location of the script.
.OUTPUTS
System.String
.NOTES
Returns the correct path within a packaged executable.
#>
[OutputType([string])]
param ()
if ($hostinvocation -ne $null)
{
Split-Path $hostinvocation.MyCommand.path
}
elseif ($script:MyInvocation.MyCommand.Path -ne $null)
{
Split-Path $script:MyInvocation.MyCommand.Path
}
else
{
$(Get-Location).path
}
}