Page 1 of 1

Compiled EXE InvocationInfo.MyCommand.Name has part of full path when hashtag (#) in folder path

Posted: Wed Jun 12, 2019 7:07 am
by DennisVH
Product, version and build: PowerShell Studio 5.6.159
32 or 64 bit version of product: 64 bit
Operating system: Windows 7 and Windows 10
32 or 64 bit OS: 64 bit

When the folder path has a # sign in it, where you run the compiled exe from, the InvocationInfo.MyCommand.Name doesn't contain just the exe file name (which is expected), but also contains the path to the parent directory that contains the # sign.

I used following script to recreate the issue:

Code: Select all

If (Test-Path -LiteralPath 'variable:HostInvocation') { $InvocationInfo = $HostInvocation }
Else { $InvocationInfo = $MyInvocation }
$InvocationInfo.MyCommand | fl
f.e. running C:\Temp\InvocationInfo\#1\x64\Invocation.exe results in
Name : #1/x64/invocationinfo.exe
where running C:\Temp\InvocationInfo\#1\Invocation.ps1 results in
Name : invocationinfo.ps1

If i run the same exe from C:\Temp\InvocationInfo\1\x64, (no hashtag), this results in
Name : invocationinfo.exe
and running C:\Temp\InvocationInfo\1\Invocation.ps1 results in
Name : invocationinfo.ps1
which is expected.

Re: Compiled EXE InvocationInfo.MyCommand.Name has part of full path when hashtag (#) in folder path

Posted: Wed Jun 12, 2019 7:46 am
by Alexander Riedel
The reason for this is that the .NET framework mangles the file name if a # character is in it.
Ran it quickly in the debugger and I see that Application.ExecutablePath delivers something like:
C:\Temp\InvocationInfo\#1/x64/PoshAppHostV5.exe

For some reason it decides mid name to switch from \ to / as a path separator.
It's not that hard to fix that in the packager engines, but I suspect that this will affect other parts of the .NET framework,
so I would just stay away from # in folder names. I have not yet researched if that is known .NET thing or not.