# PSCmdlet, how to get calling function's name

**URL:** <https://forums.powershell.org/t/pscmdlet-how-to-get-calling-functions-name/15606>\
**Category:** PowerShell Help\
**Created:** [February 9, 2021, 8:39am UTC](https://forums.powershell.org/t/pscmdlet-how-to-get-calling-functions-name/15606 "2021-02-09T08:39:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dexvx](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dexvx](https://forums.powershell.org/u/dexvx)\
**Post date:** [February 9, 2021, 8:39am UTC](https://forums.powershell.org/t/pscmdlet-how-to-get-calling-functions-name/15606/1 "2021-02-09T08:39:36Z")

</div>

Hello all. I’m writing a PSCmdlet in C# (using .NET Core 3.1 based on PS7) and I am having difficulty getting the calling function’s name within PSCmdlet.

If this were a Powershell Cmdlet, I would use the following to get the calling function name (if it exists):

```
$CallStack = Get-PSCallStack
if ($CallStack.Count -gt 1) {
    $functionName = $CallStack[1].FunctionName
}

return $functionName
```

However, if I try invoking ‘Get-PSCallStack’ in C#, it would not work because it would create a new PowerShell session and thus, there would never be a calling function:

```
using PowerShell ps = PowerShell.Create();
ps.AddScript("(Get-PSCallStack)");
Collection<PSObject> stdout = ps.Invoke();
```

Is there anyway to write a C# PSCmdlet and get the call stack when calling from Powershell sessions?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 9, 2021, 10:10am UTC](https://forums.powershell.org/t/pscmdlet-how-to-get-calling-functions-name/15606/2 "2021-02-09T10:10:55Z")

</div>

If I remember right this should be all you need:

```
$MyInvocation.MyCommand.Name.ToString()
```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 9, 2021, 10:15am UTC](https://forums.powershell.org/t/pscmdlet-how-to-get-calling-functions-name/15606/3 "2021-02-09T10:15:34Z")

</div>

In Powershell that should be enough:

```
$MyInvocation.MyCommand.Name.ToString()
```

---

<div class="post-metadata">

**Author:** ![dexvx](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dexvx](https://forums.powershell.org/u/dexvx)\
**Post date:** [February 9, 2021, 1:39pm UTC](https://forums.powershell.org/t/pscmdlet-how-to-get-calling-functions-name/15606/4 "2021-02-09T13:39:03Z")

</div>

Hello Olaf, it would appear that in the C# PSCmdlet:

```
Console.WriteLine(this.MyInvocation.MyCommand.Name);
```

The following will return the _current_ function name, not the calling function.

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![dexvx](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dexvx](https://forums.powershell.org/u/dexvx)\
**Post date:** [February 9, 2021, 1:39pm UTC](https://forums.powershell.org/t/pscmdlet-how-to-get-calling-functions-name/15606/5 "2021-02-09T13:39:14Z")

</div>

Removed; double reply.

&nbsp;

---

<div class="post-metadata">

**Author:** ![dexvx](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dexvx](https://forums.powershell.org/u/dexvx)\
**Post date:** [February 11, 2021, 8:02am UTC](https://forums.powershell.org/t/pscmdlet-how-to-get-calling-functions-name/15606/6 "2021-02-11T08:02:02Z")

</div>

So I’ve tried using the C# StackTrace, but unfortunately it just returns the C# stack trace (which in hindsight is what I should’ve expected). Workaround solution is to implement a PowerShell function that acts as a wrapper and to call the C# PSCmdlet with the PS call stack.

&nbsp;

```
$CallStack = Get-PSCallStack
if ($CallStack.Count -gt 1) {
    $functionName = $CallStack[1].FunctionName
}
 
// Assuming Get-Foo is your C# PSCmdlet that requires the PS Callstack name
Get-Foo -PSCallingFunction $functionName
```

&nbsp;

---

<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:27pm UTC](https://forums.powershell.org/t/pscmdlet-how-to-get-calling-functions-name/15606/7 "2024-05-16T20:27:54Z")

</div>

Closed on May 16, 2024, 8:27pm UTC


