Trying to get the name of my running EXE

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
MarkMelanson
Posts: 4
Last visit: Thu Feb 24, 2022 6:04 am

Trying to get the name of my running EXE

Post by MarkMelanson »

I can't seem to get the name of my compiled script. Does this look right?

Code: Select all

function Get-ScriptName
{
	if ($HostInvocation -ne $null)
	{
		$ScriptName = $HostInvocation.MyCommand.Name
	}
	else
	{
		$ScriptName = $script:MyInvocation.MyCommand.Name
	}
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Trying to get the name of my running EXE

Post by jvierra »

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){
		Split-Path $hostinvocation.MyCommand.path
	}else{
		Split-Path $script:MyInvocation.MyCommand.Path
	}
}

#Sample variable that provides the location of the script
$ScriptDirectory = Get-ScriptDirectory
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Trying to get the name of my running EXE

Post by jvierra »

In an EXE this is the name of the EXE:

$MyInvocation.MyCommand.Path
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked