# Why $PSCommandPath is $null in ISE or PowerGUI 

**URL:** <https://forums.powershell.org/t/why-pscommandpath-is-null-in-ise-or-powergui/2286>\
**Category:** PowerShell Help\
**Created:** [February 24, 2014, 5:30am UTC](https://forums.powershell.org/t/why-pscommandpath-is-null-in-ise-or-powergui/2286 "2014-02-24T05:30:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregory-suvalian](https://avatars.discourse-cdn.com/v4/letter/g/9d8465/32.png) [@gregory-suvalian](https://forums.powershell.org/u/gregory-suvalian)\
**Post date:** [February 24, 2014, 5:30am UTC](https://forums.powershell.org/t/why-pscommandpath-is-null-in-ise-or-powergui/2286/1 "2014-02-24T05:30:40Z")

</div>

Hello,

I’m trying to figure out how to find out where my script is located at the time of execution and from what I researched so far it seems to have been a problem with PS for sometime. I’m running PS 4.0 and inside ISE as well as inside PowerGui when I access $PSCommandPath variable in debug mode it always $null. The same variable works fine if I just use Write-Host $PSCommandPath.  
What I want to do is to supply certain default values for parameters like below to inform script where to look for some resource files. It works with $pwd variable but not with $PSCommandPath. How do I reliably access variable informing me where is my script is located which I’m debugging or running?

[Parameter(Mandatory=$False)]  
[string]$OutputFile = “$PSCommandPath\output\_$(Get-Date -Format (‘yyyy\_dd\_MM\_hh\_mm’)).log”,

Thanks,  
G

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [February 24, 2014, 5:48am UTC](https://forums.powershell.org/t/why-pscommandpath-is-null-in-ise-or-powergui/2286/2 "2014-02-24T05:48:15Z")

</div>

You can’t use certain automatic variables in your param block (such as $PSCommandPath, $MyInvocation, $PSCmdlet, etc); they don’t work the way you’d expect them to.

Instead, you can move that logic inside the script or function itself. For example:

```
param (
    [Parameter(Mandatory=$False)]
    [string]$OutputFile = $null
)

if (-not $OutputFile)
{
    $OutputFile = "$PSCommandPath\output_$(Get-Date -Format yyyy_dd_MM_hh_mm).log"
}
```

---

<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:52pm UTC](https://forums.powershell.org/t/why-pscommandpath-is-null-in-ise-or-powergui/2286/3 "2024-05-16T20:52:22Z")

</div>


