# Get parent function name

**URL:** <https://forums.powershell.org/t/get-parent-function-name/7113>\
**Category:** PowerShell Help\
**Created:** [August 31, 2016, 6:53pm UTC](https://forums.powershell.org/t/get-parent-function-name/7113 "2016-08-31T18:53:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![david-zemdegs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/david-zemdegs/32/1461_2.png) [@david-zemdegs](https://forums.powershell.org/u/david-zemdegs)\
**Post date:** [August 31, 2016, 6:53pm UTC](https://forums.powershell.org/t/get-parent-function-name/7113/1 "2016-08-31T18:53:42Z")

</div>

I have a script called x.ps1 which contains a function called Parent. This function calls another function called Child (which is located in a separate module).  
In the Child function I want to retrieve the name of the script and the name of the parent function.  
I can get the script name (Get-ChildItem $MyInvocation.PSCommandPath | Select -Expand Name).  
I cannot find out how to get the name of the parent function from within the child function.

Thanks

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [August 31, 2016, 8:52pm UTC](https://forums.powershell.org/t/get-parent-function-name/7113/2 "2016-08-31T20:52:39Z")

</div>

I think you are going to have to pass it in as a parameter

Here’s an example:

```
cls
function parent {
    child $MyInvocation.MyCommand, "SomeOtherStuff"
}

function child {
    Param (
        [string]$PFunc,
        [string]$OtherParam
    )
    $PFunc
    $OtherParam
}

$a = parent
$a
```

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [August 31, 2016, 9:51pm UTC](https://forums.powershell.org/t/get-parent-function-name/7113/3 "2016-08-31T21:51:30Z")

</div>

You can use Get-PSCallStack since PowerShell v3 to access information about the parent function.

```
cls
function parent {
    child 
}

function child {
    $callStack = Get-PSCallStack
    if ($callStack.Count -gt 1) {
        'Parent function: {0}' -f $callStack[1].FunctionName
    }
}

parent
```

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [August 31, 2016, 10:24pm UTC](https://forums.powershell.org/t/get-parent-function-name/7113/4 "2016-08-31T22:24:35Z")

</div>

Ooo, nice tip Daniel. That’s good stuff right there.

---

<div class="post-metadata">

**Author:** ![david-zemdegs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/david-zemdegs/32/1461_2.png) [@david-zemdegs](https://forums.powershell.org/u/david-zemdegs)\
**Post date:** [August 31, 2016, 10:51pm UTC](https://forums.powershell.org/t/get-parent-function-name/7113/5 "2016-08-31T22:51:16Z")

</div>

Thanks Daniel - exactly what I needed 🙂

---

<div class="post-metadata">

**Author:** ![nathan-todd](https://avatars.discourse-cdn.com/v4/letter/n/aeb1de/32.png) [@nathan-todd](https://forums.powershell.org/u/nathan-todd)\
**Post date:** [September 7, 2016, 11:21pm UTC](https://forums.powershell.org/t/get-parent-function-name/7113/6 "2016-09-07T23:21:50Z")

</div>

This can also be done using the $MyInvocation variable by specifying the scope:

```
function child
{
    "Parent Function Name: $((Get-Variable MyInvocation -Scope 1).Value.MyCommand.Name)"
}

function parent
{
    child
}

parent
```

Scope 0 = Current Scope  
Scope 1 = Parent Scope

[about\_Scopes](https://technet.microsoft.com/en-au/library/hh847849.aspx)

---

<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:40pm UTC](https://forums.powershell.org/t/get-parent-function-name/7113/7 "2024-05-16T20:40:33Z")

</div>

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


