# Difference between console and running a script

**URL:** <https://forums.powershell.org/t/difference-between-console-and-running-a-script/13800>\
**Category:** PowerShell Help\
**Created:** [February 12, 2020, 12:43pm UTC](https://forums.powershell.org/t/difference-between-console-and-running-a-script/13800 "2020-02-12T12:43:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shawnwat](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@shawnwat](https://forums.powershell.org/u/shawnwat)\
**Post date:** [February 12, 2020, 12:43pm UTC](https://forums.powershell.org/t/difference-between-console-and-running-a-script/13800/1 "2020-02-12T12:43:27Z")

</div>

My Google skills are weak today. I am trying to find documentation of the differences between just running commands in an open PowerShell console windows and running powershell.exe -File .\test.ps1. I know one difference is some variable are not populated like $PSScriptRoot, $PSCommandPath, $MyInvocation. I found many postings about the difference between the ISE and a real PowerShell environment. This is not what I am looking for.

Side note. [https://www.petri.com/unraveling-mystery-myinvocation](https://www.petri.com/unraveling-mystery-myinvocation) is a good explanation for $MyInvocation

Thank you

---

<div class="post-metadata">

**Author:** ![grokkit](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grokkit/32/970_2.png) [@grokkit](https://forums.powershell.org/u/grokkit)\
**Post date:** [February 12, 2020, 3:30pm UTC](https://forums.powershell.org/t/difference-between-console-and-running-a-script/13800/2 "2020-02-12T15:30:13Z")

</div>

[How to Use a Batch File to Make PowerShell Scripts Easier to Run](https://www.howtogeek.com/204088/how-to-use-a-batch-file-to-make-powershell-scripts-easier-to-run/) seems relevant. It talks about steps to make a batch file portable so that a PowerShell script can be executed reliably across different systems.

However, I couldn’t find anything that directly addresses variables not being populated when executing a script via powershell.exe. I think there may not actually be any such limitations.

I wrote a simple test script:

```
echo "MyInvocation: "$MyInvocation
echo "PSCommandPath: "$PSCommandPath
echo "PSScriptRoot: "$PSScriptRoot

pause
```

and executed it from the Start menu Run dialog:

```
powershell.exe -noprofile C:\fakepath\test.ps1
```

All three variables were populated, _except_ for the PSCommandPath and PSScriptRoot **properties** of $MyInvocation. According to the documentation for [$MyInvocation](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#myinvocation), these properties are distinctly different from the _variables_ $PSCommandPath and $PSScriptRoot. Specifically, the properties are populated _only_ if the current script was called by a previous script. So, in this case they would not be populated because the current script was executed directly (there was no calling script). The $PSCommandPath and $PSScriptRoot variables should contain path information for the current script regardless.

On a side note, I came across a few interesting things while looking for information, such as [Run PowerShell without Powershell.exe](https://medium.com/@Bank_Security/how-to-running-powershell-commands-without-powershell-exe-a6a19595f628) and [DOSfuscation: Exploring the Depths of Cmd.exe Obfuscation and Detection Techniques](https://www.fireeye.com/content/dam/fireeye-www/blog/pdfs/dosfuscation-report.pdf).

---

<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:30pm UTC](https://forums.powershell.org/t/difference-between-console-and-running-a-script/13800/3 "2024-05-16T20:30:42Z")

</div>

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


