Get-ScriptDirectory
Posted: Thu Jun 08, 2017 4:10 pm
Just a minor update to the Built-in Snippit I thought I would share.
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.
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.
- #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
- }
- }