Unix: A script without .ps1 extension passed to the powershell binary lacks invocation information ($PSCommandPath, $MyInvocation), such as when invoked via a shebang line.
See original GitHub issueSteps to reproduce
Create file ./t - note the absence of extension .ps1 - with the content below and make it executable (chmod +x ./t).
#!/usr/bin/env powershell
'$PSCommandPath: ' + $PSCommandPath
'$MyInvocation.MyCommand.Path: ' + $MyInvocation.MyCommand.Path
'$MyInvocation: ' + ($MyInvocation | Out-String)
Both the following invocation methods, from bash, yield the behavior described below:
./t # invocation via shebang line
powershell ./t # implied -File
Expected behavior
$PSCommandPath and $MyInvocation.MyCommand.Path should reflect the script’s file path, and $MyInvocation should be populated appropriately.
Actual behavior
$PSCommandPath:
$MyInvocation.MyCommand.Path:
$MyInvocation:
MyCommand : #!/usr/bin/env powershell
'$PSCommandPath: ' + $PSCommandPath
'$MyInvocation.MyCommand.Path: ' + $MyInvocation.MyCommand.Path
'$MyInvocation: ' + ($MyInvocation | Out-String)
BoundParameters : {}
UnboundArguments : {}
ScriptLineNumber : 0
OffsetInLine : 0
HistoryId : 1
ScriptName :
Line :
PositionMessage :
PSScriptRoot :
PSCommandPath :
InvocationName :
PipelineLength : 2
PipelinePosition : 1
ExpectingInput : False
CommandOrigin : Runspace
DisplayScriptPosition :
Environment data
PowerShell Core v6.0.0-beta.3 on macOS 10.12.5
PowerShell Core v6.0.0-beta.3 on Ubuntu 16.04.1 LTS
Issue Analytics
- State:
- Created 6 years ago
- Reactions:11
- Comments:22 (8 by maintainers)
Top Results From Across the Web
Unix: A script without .ps1 extension passed ...
Unix : A script without .ps1 extension passed to the powershell binary lacks invocation information ($PSCommandPath, $MyInvocation), such as when invoked via ......
Read more >How can I use a shebang in a PowerShell script?
1 Answer 1 · Ensure the pwsh or powershell command is in PATH · Use this interpreter directive: #!/usr/bin/env pwsh · Ensure the...
Read more >Untitled
2017 · Unix: A script without .ps1 extension passed to the powershell binary ... ($PSCommandPath, $MyInvocation), such as when invoked via a shebang...
Read more >Do executable shebang scripts always require LF line ...
newlines - Do executable shebang scripts always require LF line endings? - Unix & Linux Stack Exchange.
Read more >Creating ESM-based shell scripts for Unix and Windows ...
In this blog post, we learn how to implement shell scripts via Node.js ESM modules. There are two common ways of doing so:....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Reopen the issue as there is an open PR for this.
@iSazonov, of course there is:
A script should provide invocation information irrespective of the specific invocation method.
The proposed workaround is only a workaround from within PowerShell and would also switch from running in a child process to running in-process; when called from outside PowerShell, you’d have to call the script with the
.ps1extension, whereas the primary advantage of a shebang-line-based script is to not require a filename extension.@SteveL-MSFT’s statement is open to interpretation, but I read it to mean: “While the current behavior is somewhat intentional (in the sense that there is a code path for it), it should be changed - here’s the relevant source-code location for someone who wants to work on this.” @SteveL-MSFT, can you confirm?
Either way, the current behavior is clearly a bug that should be fixed, so please reopen this issue.