$PsScriptRoot only works with Modules - by qa_warrior

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 522951 Comments
Status Closed Workarounds
Type Bug Repros 1
Opened 1/3/2010 8:59:07 AM
Access Restriction Public


Powershell provides lots of built in or automatic variables one of those new to v2.0 is the PsScriptRoot variable. You would think that with this name it would give you the Root of the Script that is running….Well sort of it only works in psm1 or module file. Why is it not $PsModuleRoot then. Finding the root of the script executing is a common action and it was nice to include this variable but why not for any executing powershell so I am left to keep using $MyInvocation.$MyCommand.Path for this purpose
Sign in to post a comment.
Posted by Dan [MSFT] on 8/2/2012 at 7:02 PM
# This is fixed in PowerShell 3.0
# We added a new variable called $PSCommandPath
# This holds the full path to the file
# Works in conjunction with existing $PSScriptRoot

# Create a test script module that shows $PSScriptRoot and $PSCommandPath
'Write-Host "`$PSScriptRoot is:`t" $PSScriptroot; Write-Host "`$PSCommandPath is:`t" $PSCommandPath' > $Env:tmp\CmdPath.psm1
# $PSScriptRoot is the full path to the containing folder
# $PSCommandPath is the full path to the file
# Import the module to see the difference
Import-Module $Env:tmp\CmdPath.psm1
# This works from scripts (.ps1) as well as script modules (.psm1)
# Try it from a script
'"`$PSScriptRoot is:`t" + $PSScriptroot; "`$PSCommandPath is:`t" + $PSCommandPath' > $Env:tmp\CmdPath.ps1
& $Env:tmp\CmdPath.ps1
Posted by mosaic on 12/18/2011 at 11:33 AM
Re. "Why is it not $PsModuleRoot then":

Quote from Windows PowerShell in Action, second edition page 358:
You’d think the correct name for this variable would be $PSModuleRoot, not $PSScriptRoot. As it happens, the original idea
was that this variable would also be available in regular scripts, not just modules. Unfortunately that was never implemented, but the name wasn’t changed either. And thus we have another question for the PowerShell
trivia contest
Posted by Stephen Mills on 1/6/2010 at 12:35 PM
Maybe a new variable should be added instead. Something like $PsCurrentScriptPath that gives the same information as $MyInvocation.MyCommand.Path.

I don't really like the idea of $PSModuleRoot being changed. It was added specifically for modules so that your scripts can be written for where the root of the module is, instead of having to do some sort of install for modules. Maybe it should have been called $PsModuleRoot, but it wasn't. I use it to host SQL files inside a module. I.e. "$PsScriptRoot\SQL\Test.sql". You could also do the same thing with Powershell scripts ( "$PsScriptRoot\Script\Get-Test.ps1" ). That way you can separate the different items without a headache.
Posted by Roman Kuzmin on 1/3/2010 at 10:47 AM
Here is a similar suggestion: 517862