# $MyInvocation.MyCommand.Name is empty?

**URL:** <https://forums.powershell.org/t/myinvocation-mycommand-name-is-empty/19297>\
**Category:** PowerShell Help\
**Created:** [May 12, 2022, 2:19pm UTC](https://forums.powershell.org/t/myinvocation-mycommand-name-is-empty/19297 "2022-05-12T14:19:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Sylvest](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@David\_Sylvest](https://forums.powershell.org/u/David_Sylvest)\
**Post date:** [May 12, 2022, 2:19pm UTC](https://forums.powershell.org/t/myinvocation-mycommand-name-is-empty/19297/1 "2022-05-12T14:19:38Z")

</div>

Hi.

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.

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [May 12, 2022, 3:30pm UTC](https://forums.powershell.org/t/myinvocation-mycommand-name-is-empty/19297/2 "2022-05-12T15:30:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![David\_Sylvest](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@David\_Sylvest](https://forums.powershell.org/u/David_Sylvest)\
**Post date:** [May 16, 2022, 9:25am UTC](https://forums.powershell.org/t/myinvocation-mycommand-name-is-empty/19297/3 "2022-05-16T09:25:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [May 16, 2022, 4:15pm UTC](https://forums.powershell.org/t/myinvocation-mycommand-name-is-empty/19297/4 "2022-05-16T16:15:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![David\_Sylvest](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@David\_Sylvest](https://forums.powershell.org/u/David_Sylvest)\
**Post date:** [May 18, 2022, 7:52am UTC](https://forums.powershell.org/t/myinvocation-mycommand-name-is-empty/19297/5 "2022-05-18T07:52:35Z")

</div>

Thats from running $MyInvocation and $MyInvocation.MyCommand

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

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bb8d78da31509b5df266cf73130d67cebb11fe59.png)

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [May 18, 2022, 9:06am UTC](https://forums.powershell.org/t/myinvocation-mycommand-name-is-empty/19297/6 "2022-05-18T09:06:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![David\_Sylvest](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@David\_Sylvest](https://forums.powershell.org/u/David_Sylvest)\
**Post date:** [May 18, 2022, 9:36am UTC](https://forums.powershell.org/t/myinvocation-mycommand-name-is-empty/19297/7 "2022-05-18T09:36:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/myinvocation-mycommand-name-is-empty/19297/8 "2024-05-16T20:17:57Z")

</div>


