与 C 的 argv[0] 等效的 Powershell 是什么?

What is the Powershell equivalent to C's argv[0]?

也就是说 - 我怎样才能从所述脚本中获取被调用脚本的名称?例如,如果我这样做:

.\foobar.ps1 -param1 10 -param2 20

如何获取字符串 'foobar.ps1'?

有个automatic variable that provides this information: $PSCommandPath.

这给出了脚本的完整路径,但如果您只需要文件名,则可以使用 Split-Path 来获取该部分:

Split-Path -Path $PSCommandPath -Leaf