Skip to content

Instantly share code, notes, and snippets.

@tmeers
Created July 17, 2019 13:18
Show Gist options
  • Save tmeers/fc286e4e6d47f2fbfe9ab15b1f740bd0 to your computer and use it in GitHub Desktop.
Save tmeers/fc286e4e6d47f2fbfe9ab15b1f740bd0 to your computer and use it in GitHub Desktop.
Returns script line number and file name for debugging
# Thanks to this post for showing how to find the current line number and file when called in a script
# https://poshoholic.com/2009/01/19/powershell-quick-tip-how-to-retrieve-the-current-line-number-and-file-name-in-your-powershell-script/
# Slightly extented with __WHERE__ to shorten everything up a bit
#
# Calling __WHERE__ will format the output while still returning the correct values.
function Get-CurrentLineNumber {
$MyInvocation.ScriptLineNumber
}
function Get-CurrentFileName {
$MyInvocation.ScriptName
}
function Get-CurrentFileNameAndLine {
"Line: {0} File: {1}" -f $MyInvocation.ScriptLineNumber,$MyInvocation.ScriptName
}
New-Alias -Name __LINE__ -Value Get-CurrentLineNumber –Description 'Returns the current line number in a PowerShell script file.' -ErrorAction SilentlyContinue
New-Alias -Name __FILE__ -Value Get-CurrentFileName -Description 'Returns the name of the current PowerShell script file.' -ErrorAction SilentlyContinue
New-Alias -Name __WHERE__ -Value Get-CurrentFileNameAndLine -Description 'Returns the name and line number of the current PowerShell script file.' -ErrorAction SilentlyContinue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment