Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script filename 2

Status
Not open for further replies.

Kazendar

Programmer
Oct 23, 2008
18
LB
How to get the script-filename inside the running script ?
 
Use the built-in variable $MyInvocation (more specifically $MyInvocation.ScriptName). It's not exact because the value changes each time a function or script is called. You'd have to call it fairly early in the script, or you may have to go through all of the available scopes (which PowerShell keeps track of) to get to the top scope and then read it. Information in chapter 9 of Bruce Payette's book "Windows PowerShell in Action" as well as in Microsoft's documentation.
 
Thanks, it works.
------------------------------------
Script content :

# c:\scripts\test.ps1
$MyInvocation.MyCommand.Name # test.ps1
$MyInvocation.MyCommand.Definition # c:\scripts\test.ps1
-------------------------------------
Running :
PowerShell.exe "& {c:\scripts\test.ps1}"

Results :
test.ps1
c:\scripts\test.ps1
---------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top