$MyInvocation.MyCommand.Name is empty?


I am trying to get the name of the current running script, and i googled my way to $MyInvocation.MyCommand.Name should do the trick, however i have no name in there. It’s just a an empty field.

how come? and/or how could i then get the name of the script i an currently running?

I want to use it for logging.

How are you running it. Just as a script ? or any kind of background job ? Can you share little more info ?

I am writing a script file (.ps1) where i use VScode, i’ve tried to run the $MyInvocation.MyCommand.Name in the terminal window and execute the command from within the .ps1 file. I get an empty output in both places.

What i am trying to accomplish is to create a log fuction, that writes in the logfile what script it is logging from.

just be clear, can you share the exact statement of printing $MyInvocation.MyCommand.Name ?

Thats from running $MyInvocation and $MyInvocation.MyCommand

So i guess the exact statement of printing it, is empty ?

When you run from the editor, its not gonna have any value. Because its not executing the ps1 file.
Try executing .\filename.ps1.

1 Like

Damn that is a vital peace of information
Thanks a bunch, now it works! :slight_smile:

1 Like