Windows PowerShell Home
$PsScriptRoot only works with Modules
1/3/2010 8:59:07 AM
User(s) can reproduce this bug
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
How often does this happen?
Have you seen this problem before in this product?
No, this is new to the most recent version
get the value of $PsScriptRoot inside a running .ps1 file and will be null. Get the value from a running .psm1 file and will return the full path to the file
PsScriptRoot should be set for any executing powershell psm1, ps1 and any future code collection conventions.
to post a comment.
Please enter a comment.
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
# 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
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
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.
on 1/3/2010 at 10:47 AM
Here is a similar suggestion: 517862
to post a workaround.
Please enter a workaround.
Joel 'Jaykul' Bennett
on 7/16/2010 at 8:35 AM
# Make this the first line in your script after the param block:
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
© 2014 Microsoft