What's the best way to determine the location of the current PowerShell script?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Whenever I need to reference a common module or script, I like to use paths relative to the current script file, that way, my script can always find other scripts in the library.
So, what is the best, standard way of determining the directory of the current script? Currently, I'm doing:
$MyDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
I know in modules (.psm1) you can use $PSScriptRoot
to get this information, but that doesn't get set in regular scripts (i.e. .ps1 files).
What's the canonical way to get the current PowerShell script file's location?
powershell powershell-v2.0
add a comment |
Whenever I need to reference a common module or script, I like to use paths relative to the current script file, that way, my script can always find other scripts in the library.
So, what is the best, standard way of determining the directory of the current script? Currently, I'm doing:
$MyDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
I know in modules (.psm1) you can use $PSScriptRoot
to get this information, but that doesn't get set in regular scripts (i.e. .ps1 files).
What's the canonical way to get the current PowerShell script file's location?
powershell powershell-v2.0
1
possible duplicate of How can i get the file system location of a powershell script?
– JohnC
Apr 14 '14 at 11:09
add a comment |
Whenever I need to reference a common module or script, I like to use paths relative to the current script file, that way, my script can always find other scripts in the library.
So, what is the best, standard way of determining the directory of the current script? Currently, I'm doing:
$MyDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
I know in modules (.psm1) you can use $PSScriptRoot
to get this information, but that doesn't get set in regular scripts (i.e. .ps1 files).
What's the canonical way to get the current PowerShell script file's location?
powershell powershell-v2.0
Whenever I need to reference a common module or script, I like to use paths relative to the current script file, that way, my script can always find other scripts in the library.
So, what is the best, standard way of determining the directory of the current script? Currently, I'm doing:
$MyDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
I know in modules (.psm1) you can use $PSScriptRoot
to get this information, but that doesn't get set in regular scripts (i.e. .ps1 files).
What's the canonical way to get the current PowerShell script file's location?
powershell powershell-v2.0
powershell powershell-v2.0
edited Mar 16 '18 at 14:00
Servy
179k18241356
179k18241356
asked Mar 28 '11 at 23:46
Aaron JensenAaron Jensen
14.8k96175
14.8k96175
1
possible duplicate of How can i get the file system location of a powershell script?
– JohnC
Apr 14 '14 at 11:09
add a comment |
1
possible duplicate of How can i get the file system location of a powershell script?
– JohnC
Apr 14 '14 at 11:09
1
1
possible duplicate of How can i get the file system location of a powershell script?
– JohnC
Apr 14 '14 at 11:09
possible duplicate of How can i get the file system location of a powershell script?
– JohnC
Apr 14 '14 at 11:09
add a comment |
12 Answers
12
active
oldest
votes
PowerShell 3+
# This is an automatic variable set to the current file's/module's directory
$PSScriptRoot
PowerShell 2
Prior to PowerShell 3, there was not a better way than querying the
MyInvocation.MyCommand.Definition
property for general scripts. I had the
following line at the top of essentially every powershell script I had:
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
1
What doesSplit-Path
used for here?
– CMCDragonkai
Dec 9 '16 at 9:06
4
Split-Path
is used with the-Parent
parameter to return the current directory without the currently executing script's name.
– hjoelr
Dec 9 '16 at 14:55
2
Note: with PowerShell on Linux/macOS, your script must have a .ps1 extension for PSScriptRoot/MyInvocation etc to be populated. See bug report here: github.com/PowerShell/PowerShell/issues/4217
– Dave Wood
Mar 11 '18 at 5:11
add a comment |
If you are creating a V2 Module, you can use an automatic variable called
$PSScriptRoot
.
From PS > Help automatic_variable
$PSScriptRoot
Contains the directory from which the script module is being executed.
This variable allows scripts to use the module path to access other
resources.
10
This is what you need in PS 3.0:$PSCommandPath Contains the full path and file name of the script that is being run. This variable is valid in all scripts.
– CodeMonkeyKing
Mar 14 '13 at 6:02
1
Just tested $PSScriptRoot and working as expected. However, it would give you an empty string if you run it at command line. It would only give you result if used in a script and script is executed. That's what it is meant for .....
– Farrukh Waheed
Sep 17 '13 at 4:22
4
I'm confused. This answer says to use PSScriptRoot for V2. Another answer says PSScriptRoot is for V3+, and to use something different for v2.
– user1499731
May 9 '14 at 16:35
5
@user $PSScriptRoot in v2 is only for modules, if you're writing 'normal' scripts not in a module you need $MyInvocation.MyCommand.Definition, see top answer.
– yzorg
Jan 16 '15 at 15:01
1
@Lofful I said in "v2" it was only defined for modules. You're saying it's defined outside modules in v3. I think we're saying the same thing. : )
– yzorg
Aug 1 '16 at 13:59
|
show 3 more comments
For PowerShell 3.0
$PSCommandPath
Contains the full path and file name of the script that is being run.
This variable is valid in all scripts.
The function is then:
function Get-ScriptDirectory {
Split-Path -Parent $PSCommandPath
}
17
Even better, use $PSScriptRoot. It is the current file's/module's directory.
– Aaron Jensen
Apr 29 '14 at 17:57
1
This command includes the filename of the script, which threw me off until I realized that. When you are wanting the path, you probably don't want the script name in there too. At least, I can't think of a reason you would want that. $PSScriptRoot does not include the filename (gleaned from other answers).
– YetAnotherRandomUser
Jun 21 '17 at 11:26
1
Split-Path -Parent $PSCommandPath
did the job, thank you
– Yoraco Gonzales
Sep 8 '18 at 18:41
add a comment |
Maybe i'm missing something here... but if you want the present working directory you can just use this: (Get-Location).Path
for a string, or Get-Location
for an object.
Unless you're referring to something like this, which I understand after reading the question again.
function Get-Script-Directory
{
$scriptInvocation = (Get-Variable MyInvocation -Scope 1).Value
return Split-Path $scriptInvocation.MyCommand.Path
}
15
This gets the current location where the user is running the script. Not the location of the script file itself.
– Aaron Jensen
Mar 29 '11 at 0:07
2
function Get-Script-Directory { $scriptInvocation = (Get-Variable MyInvocation -Scope 1).Value return Split-Path $scriptInvocation.MyCommand.Path } $hello = "hello" Write-Host (Get-Script-Directory) Write-Host $hello
Save that and run it from a different directory. You'll show the path to the script.
– Sean C.
Mar 29 '11 at 0:25
That's a good function, and does what I need, but how do I share it and use it in all my scripts? It's a chicken and egg problem: I'd like to use a function to find out my current location, but I need my location to load the function.
– Aaron Jensen
Mar 29 '11 at 2:26
2
NOTE: The invokation of this function must be at the top level of your script, if it is nested within another function, then you have to change the "-Scope" parameter to designate how deep in the call stack you are.
– kenny
Jun 1 '15 at 21:57
add a comment |
For Powershell 3+
function Get-ScriptDirectory {
if ($psise) {Split-Path $psise.CurrentFile.FullPath}
else {$global:PSScriptRoot}
}
I've placed this function in my profile. Works in ISE using F8/Run Selection too.
1
Nice to see a solution that also works in ISE
– KyleMit
Nov 1 '18 at 17:27
add a comment |
Very similar to already posted answers, but piping seems more PS-like.
$PSCommandPath | Split-Path -Parent
add a comment |
I use the automatic variable $ExecutionContext, it works from PowerShell 2 and later.
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('.')
$ExecutionContext
Contains an EngineIntrinsics object that represents the
execution context of the Windows PowerShell host. You can
use this variable to find the execution objects that are
available to cmdlets.
1
This is the only one it worked for me, trying to feed powershell from STDIN.
– Sebastian
Apr 15 '17 at 16:45
This solution also works properly when you are in a UNC path context.
– user2030503
Feb 12 '18 at 7:50
1
This is apparently picking up the working directory - rather than where the script is located ?
– monojohnny
Nov 9 '18 at 12:05
@monojohnny Yes this is basically the current working directory and will not work when calling a script from another location.
– marsze
Jan 24 at 11:26
add a comment |
I needed to know the script name and where it is executing from.
Prefixing "$global:" to the MyInvocation structure returns the full path and script name when called from both the main script, and the main line of an imported .PSM1 library file. It also works from within a function in an imported library.
After much fiddling around, I settled on using $global:MyInvocation.InvocationName.
It works reliably with CMD launch, Run With Powershell, and the ISE.
Both local and UNC launches return the correct path.
4
Split-Path -Path $($global:MyInvocation.MyCommand.Path) worked perfect thanks. The other solutions returned the path of the calling application.
– C0r3yh
May 6 '14 at 21:24
1
Trivial note: in ISE calling this function using F8/Run Selection will trigger aParameterArgumentValidationErrorNullNotAllowed
exception.
– weir
Oct 19 '15 at 14:15
add a comment |
Took me a while to develop something that took the accepted answer and turned it into a robust function.
Not sure about others but I work in an environment with machines on both PowerShell version 2 and 3 so I needed to handle both. The following function offers a graceful fallback:
Function Get-PSScriptRoot
{
$ScriptRoot = ""
Try
{
$ScriptRoot = Get-Variable -Name PSScriptRoot -ValueOnly -ErrorAction Stop
}
Catch
{
$ScriptRoot = Split-Path $script:MyInvocation.MyCommand.Path
}
Write-Output $ScriptRoot
}
It also means that the function refers to the Script scope rather than the parent's scope as outlined by Michael Sorens in his blog
Thanks! The "$script:" was what I needed to get this to work in Windows PowerShell ISE.
– Kirk Liemohn
Jan 11 '17 at 20:20
add a comment |
You might also consider split-path -parent $psISE.CurrentFile.Fullpath
if any of the other methods fail. In particular, if you run a file to load a bunch of functions and then execute those functions with-in the ISE shell (or if you run-selected), it seems the Get-Script-Directory
function as above doesn't work.
3
$PSCommandPath
will work in the ISE as long as you save the script first and execute the whole file. Otherwise, you're not actually executing a script; you're just "pasting" commands into the shell.
– Zenexer
Jul 25 '13 at 4:02
@Zenexer I think that was my goal at the time. Although if my goal didn't match up with the original one, this might not be too helpful except to the occasional Googlers...
– user1499731
Jan 16 '15 at 16:36
add a comment |
Found that the older solutions posted here didn't work for me on PowerShell V5. I came up with this:
try {
$scriptPath = $PSScriptRoot
if (!$scriptPath)
{
if ($psISE)
{
$scriptPath = Split-Path -Parent -Path $psISE.CurrentFile.FullPath
} else {
Write-Host -ForegroundColor Red "Cannot resolve script file's path"
exit 1
}
}
} catch {
Write-Host -ForegroundColor Red "Caught Exception: $($Error[0].Exception.Message)"
exit 2
}
Write-Host "Path: $scriptPath"
HTH
add a comment |
function func1()
{
$inv = (Get-Variable MyInvocation -Scope 1).Value
#$inv.MyCommand | Format-List *
$Path1 = Split-Path $inv.scriptname
Write-Host $Path1
}
function Main()
{
func1
}
Main
add a comment |
protected by Community♦ Mar 15 '17 at 0:36
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
12 Answers
12
active
oldest
votes
12 Answers
12
active
oldest
votes
active
oldest
votes
active
oldest
votes
PowerShell 3+
# This is an automatic variable set to the current file's/module's directory
$PSScriptRoot
PowerShell 2
Prior to PowerShell 3, there was not a better way than querying the
MyInvocation.MyCommand.Definition
property for general scripts. I had the
following line at the top of essentially every powershell script I had:
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
1
What doesSplit-Path
used for here?
– CMCDragonkai
Dec 9 '16 at 9:06
4
Split-Path
is used with the-Parent
parameter to return the current directory without the currently executing script's name.
– hjoelr
Dec 9 '16 at 14:55
2
Note: with PowerShell on Linux/macOS, your script must have a .ps1 extension for PSScriptRoot/MyInvocation etc to be populated. See bug report here: github.com/PowerShell/PowerShell/issues/4217
– Dave Wood
Mar 11 '18 at 5:11
add a comment |
PowerShell 3+
# This is an automatic variable set to the current file's/module's directory
$PSScriptRoot
PowerShell 2
Prior to PowerShell 3, there was not a better way than querying the
MyInvocation.MyCommand.Definition
property for general scripts. I had the
following line at the top of essentially every powershell script I had:
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
1
What doesSplit-Path
used for here?
– CMCDragonkai
Dec 9 '16 at 9:06
4
Split-Path
is used with the-Parent
parameter to return the current directory without the currently executing script's name.
– hjoelr
Dec 9 '16 at 14:55
2
Note: with PowerShell on Linux/macOS, your script must have a .ps1 extension for PSScriptRoot/MyInvocation etc to be populated. See bug report here: github.com/PowerShell/PowerShell/issues/4217
– Dave Wood
Mar 11 '18 at 5:11
add a comment |
PowerShell 3+
# This is an automatic variable set to the current file's/module's directory
$PSScriptRoot
PowerShell 2
Prior to PowerShell 3, there was not a better way than querying the
MyInvocation.MyCommand.Definition
property for general scripts. I had the
following line at the top of essentially every powershell script I had:
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
PowerShell 3+
# This is an automatic variable set to the current file's/module's directory
$PSScriptRoot
PowerShell 2
Prior to PowerShell 3, there was not a better way than querying the
MyInvocation.MyCommand.Definition
property for general scripts. I had the
following line at the top of essentially every powershell script I had:
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
edited May 23 '17 at 12:34
Community♦
11
11
answered Mar 28 '11 at 23:50
JaredParJaredPar
584k12110791355
584k12110791355
1
What doesSplit-Path
used for here?
– CMCDragonkai
Dec 9 '16 at 9:06
4
Split-Path
is used with the-Parent
parameter to return the current directory without the currently executing script's name.
– hjoelr
Dec 9 '16 at 14:55
2
Note: with PowerShell on Linux/macOS, your script must have a .ps1 extension for PSScriptRoot/MyInvocation etc to be populated. See bug report here: github.com/PowerShell/PowerShell/issues/4217
– Dave Wood
Mar 11 '18 at 5:11
add a comment |
1
What doesSplit-Path
used for here?
– CMCDragonkai
Dec 9 '16 at 9:06
4
Split-Path
is used with the-Parent
parameter to return the current directory without the currently executing script's name.
– hjoelr
Dec 9 '16 at 14:55
2
Note: with PowerShell on Linux/macOS, your script must have a .ps1 extension for PSScriptRoot/MyInvocation etc to be populated. See bug report here: github.com/PowerShell/PowerShell/issues/4217
– Dave Wood
Mar 11 '18 at 5:11
1
1
What does
Split-Path
used for here?– CMCDragonkai
Dec 9 '16 at 9:06
What does
Split-Path
used for here?– CMCDragonkai
Dec 9 '16 at 9:06
4
4
Split-Path
is used with the -Parent
parameter to return the current directory without the currently executing script's name.– hjoelr
Dec 9 '16 at 14:55
Split-Path
is used with the -Parent
parameter to return the current directory without the currently executing script's name.– hjoelr
Dec 9 '16 at 14:55
2
2
Note: with PowerShell on Linux/macOS, your script must have a .ps1 extension for PSScriptRoot/MyInvocation etc to be populated. See bug report here: github.com/PowerShell/PowerShell/issues/4217
– Dave Wood
Mar 11 '18 at 5:11
Note: with PowerShell on Linux/macOS, your script must have a .ps1 extension for PSScriptRoot/MyInvocation etc to be populated. See bug report here: github.com/PowerShell/PowerShell/issues/4217
– Dave Wood
Mar 11 '18 at 5:11
add a comment |
If you are creating a V2 Module, you can use an automatic variable called
$PSScriptRoot
.
From PS > Help automatic_variable
$PSScriptRoot
Contains the directory from which the script module is being executed.
This variable allows scripts to use the module path to access other
resources.
10
This is what you need in PS 3.0:$PSCommandPath Contains the full path and file name of the script that is being run. This variable is valid in all scripts.
– CodeMonkeyKing
Mar 14 '13 at 6:02
1
Just tested $PSScriptRoot and working as expected. However, it would give you an empty string if you run it at command line. It would only give you result if used in a script and script is executed. That's what it is meant for .....
– Farrukh Waheed
Sep 17 '13 at 4:22
4
I'm confused. This answer says to use PSScriptRoot for V2. Another answer says PSScriptRoot is for V3+, and to use something different for v2.
– user1499731
May 9 '14 at 16:35
5
@user $PSScriptRoot in v2 is only for modules, if you're writing 'normal' scripts not in a module you need $MyInvocation.MyCommand.Definition, see top answer.
– yzorg
Jan 16 '15 at 15:01
1
@Lofful I said in "v2" it was only defined for modules. You're saying it's defined outside modules in v3. I think we're saying the same thing. : )
– yzorg
Aug 1 '16 at 13:59
|
show 3 more comments
If you are creating a V2 Module, you can use an automatic variable called
$PSScriptRoot
.
From PS > Help automatic_variable
$PSScriptRoot
Contains the directory from which the script module is being executed.
This variable allows scripts to use the module path to access other
resources.
10
This is what you need in PS 3.0:$PSCommandPath Contains the full path and file name of the script that is being run. This variable is valid in all scripts.
– CodeMonkeyKing
Mar 14 '13 at 6:02
1
Just tested $PSScriptRoot and working as expected. However, it would give you an empty string if you run it at command line. It would only give you result if used in a script and script is executed. That's what it is meant for .....
– Farrukh Waheed
Sep 17 '13 at 4:22
4
I'm confused. This answer says to use PSScriptRoot for V2. Another answer says PSScriptRoot is for V3+, and to use something different for v2.
– user1499731
May 9 '14 at 16:35
5
@user $PSScriptRoot in v2 is only for modules, if you're writing 'normal' scripts not in a module you need $MyInvocation.MyCommand.Definition, see top answer.
– yzorg
Jan 16 '15 at 15:01
1
@Lofful I said in "v2" it was only defined for modules. You're saying it's defined outside modules in v3. I think we're saying the same thing. : )
– yzorg
Aug 1 '16 at 13:59
|
show 3 more comments
If you are creating a V2 Module, you can use an automatic variable called
$PSScriptRoot
.
From PS > Help automatic_variable
$PSScriptRoot
Contains the directory from which the script module is being executed.
This variable allows scripts to use the module path to access other
resources.
If you are creating a V2 Module, you can use an automatic variable called
$PSScriptRoot
.
From PS > Help automatic_variable
$PSScriptRoot
Contains the directory from which the script module is being executed.
This variable allows scripts to use the module path to access other
resources.
edited May 19 '14 at 1:38
Steven Penny
1
1
answered Mar 29 '11 at 3:17
Andy SchneiderAndy Schneider
6,71952947
6,71952947
10
This is what you need in PS 3.0:$PSCommandPath Contains the full path and file name of the script that is being run. This variable is valid in all scripts.
– CodeMonkeyKing
Mar 14 '13 at 6:02
1
Just tested $PSScriptRoot and working as expected. However, it would give you an empty string if you run it at command line. It would only give you result if used in a script and script is executed. That's what it is meant for .....
– Farrukh Waheed
Sep 17 '13 at 4:22
4
I'm confused. This answer says to use PSScriptRoot for V2. Another answer says PSScriptRoot is for V3+, and to use something different for v2.
– user1499731
May 9 '14 at 16:35
5
@user $PSScriptRoot in v2 is only for modules, if you're writing 'normal' scripts not in a module you need $MyInvocation.MyCommand.Definition, see top answer.
– yzorg
Jan 16 '15 at 15:01
1
@Lofful I said in "v2" it was only defined for modules. You're saying it's defined outside modules in v3. I think we're saying the same thing. : )
– yzorg
Aug 1 '16 at 13:59
|
show 3 more comments
10
This is what you need in PS 3.0:$PSCommandPath Contains the full path and file name of the script that is being run. This variable is valid in all scripts.
– CodeMonkeyKing
Mar 14 '13 at 6:02
1
Just tested $PSScriptRoot and working as expected. However, it would give you an empty string if you run it at command line. It would only give you result if used in a script and script is executed. That's what it is meant for .....
– Farrukh Waheed
Sep 17 '13 at 4:22
4
I'm confused. This answer says to use PSScriptRoot for V2. Another answer says PSScriptRoot is for V3+, and to use something different for v2.
– user1499731
May 9 '14 at 16:35
5
@user $PSScriptRoot in v2 is only for modules, if you're writing 'normal' scripts not in a module you need $MyInvocation.MyCommand.Definition, see top answer.
– yzorg
Jan 16 '15 at 15:01
1
@Lofful I said in "v2" it was only defined for modules. You're saying it's defined outside modules in v3. I think we're saying the same thing. : )
– yzorg
Aug 1 '16 at 13:59
10
10
This is what you need in PS 3.0:
$PSCommandPath Contains the full path and file name of the script that is being run. This variable is valid in all scripts.
– CodeMonkeyKing
Mar 14 '13 at 6:02
This is what you need in PS 3.0:
$PSCommandPath Contains the full path and file name of the script that is being run. This variable is valid in all scripts.
– CodeMonkeyKing
Mar 14 '13 at 6:02
1
1
Just tested $PSScriptRoot and working as expected. However, it would give you an empty string if you run it at command line. It would only give you result if used in a script and script is executed. That's what it is meant for .....
– Farrukh Waheed
Sep 17 '13 at 4:22
Just tested $PSScriptRoot and working as expected. However, it would give you an empty string if you run it at command line. It would only give you result if used in a script and script is executed. That's what it is meant for .....
– Farrukh Waheed
Sep 17 '13 at 4:22
4
4
I'm confused. This answer says to use PSScriptRoot for V2. Another answer says PSScriptRoot is for V3+, and to use something different for v2.
– user1499731
May 9 '14 at 16:35
I'm confused. This answer says to use PSScriptRoot for V2. Another answer says PSScriptRoot is for V3+, and to use something different for v2.
– user1499731
May 9 '14 at 16:35
5
5
@user $PSScriptRoot in v2 is only for modules, if you're writing 'normal' scripts not in a module you need $MyInvocation.MyCommand.Definition, see top answer.
– yzorg
Jan 16 '15 at 15:01
@user $PSScriptRoot in v2 is only for modules, if you're writing 'normal' scripts not in a module you need $MyInvocation.MyCommand.Definition, see top answer.
– yzorg
Jan 16 '15 at 15:01
1
1
@Lofful I said in "v2" it was only defined for modules. You're saying it's defined outside modules in v3. I think we're saying the same thing. : )
– yzorg
Aug 1 '16 at 13:59
@Lofful I said in "v2" it was only defined for modules. You're saying it's defined outside modules in v3. I think we're saying the same thing. : )
– yzorg
Aug 1 '16 at 13:59
|
show 3 more comments
For PowerShell 3.0
$PSCommandPath
Contains the full path and file name of the script that is being run.
This variable is valid in all scripts.
The function is then:
function Get-ScriptDirectory {
Split-Path -Parent $PSCommandPath
}
17
Even better, use $PSScriptRoot. It is the current file's/module's directory.
– Aaron Jensen
Apr 29 '14 at 17:57
1
This command includes the filename of the script, which threw me off until I realized that. When you are wanting the path, you probably don't want the script name in there too. At least, I can't think of a reason you would want that. $PSScriptRoot does not include the filename (gleaned from other answers).
– YetAnotherRandomUser
Jun 21 '17 at 11:26
1
Split-Path -Parent $PSCommandPath
did the job, thank you
– Yoraco Gonzales
Sep 8 '18 at 18:41
add a comment |
For PowerShell 3.0
$PSCommandPath
Contains the full path and file name of the script that is being run.
This variable is valid in all scripts.
The function is then:
function Get-ScriptDirectory {
Split-Path -Parent $PSCommandPath
}
17
Even better, use $PSScriptRoot. It is the current file's/module's directory.
– Aaron Jensen
Apr 29 '14 at 17:57
1
This command includes the filename of the script, which threw me off until I realized that. When you are wanting the path, you probably don't want the script name in there too. At least, I can't think of a reason you would want that. $PSScriptRoot does not include the filename (gleaned from other answers).
– YetAnotherRandomUser
Jun 21 '17 at 11:26
1
Split-Path -Parent $PSCommandPath
did the job, thank you
– Yoraco Gonzales
Sep 8 '18 at 18:41
add a comment |
For PowerShell 3.0
$PSCommandPath
Contains the full path and file name of the script that is being run.
This variable is valid in all scripts.
The function is then:
function Get-ScriptDirectory {
Split-Path -Parent $PSCommandPath
}
For PowerShell 3.0
$PSCommandPath
Contains the full path and file name of the script that is being run.
This variable is valid in all scripts.
The function is then:
function Get-ScriptDirectory {
Split-Path -Parent $PSCommandPath
}
edited May 19 '14 at 1:39
Steven Penny
1
1
answered Mar 14 '13 at 6:05
CodeMonkeyKingCodeMonkeyKing
3,70612534
3,70612534
17
Even better, use $PSScriptRoot. It is the current file's/module's directory.
– Aaron Jensen
Apr 29 '14 at 17:57
1
This command includes the filename of the script, which threw me off until I realized that. When you are wanting the path, you probably don't want the script name in there too. At least, I can't think of a reason you would want that. $PSScriptRoot does not include the filename (gleaned from other answers).
– YetAnotherRandomUser
Jun 21 '17 at 11:26
1
Split-Path -Parent $PSCommandPath
did the job, thank you
– Yoraco Gonzales
Sep 8 '18 at 18:41
add a comment |
17
Even better, use $PSScriptRoot. It is the current file's/module's directory.
– Aaron Jensen
Apr 29 '14 at 17:57
1
This command includes the filename of the script, which threw me off until I realized that. When you are wanting the path, you probably don't want the script name in there too. At least, I can't think of a reason you would want that. $PSScriptRoot does not include the filename (gleaned from other answers).
– YetAnotherRandomUser
Jun 21 '17 at 11:26
1
Split-Path -Parent $PSCommandPath
did the job, thank you
– Yoraco Gonzales
Sep 8 '18 at 18:41
17
17
Even better, use $PSScriptRoot. It is the current file's/module's directory.
– Aaron Jensen
Apr 29 '14 at 17:57
Even better, use $PSScriptRoot. It is the current file's/module's directory.
– Aaron Jensen
Apr 29 '14 at 17:57
1
1
This command includes the filename of the script, which threw me off until I realized that. When you are wanting the path, you probably don't want the script name in there too. At least, I can't think of a reason you would want that. $PSScriptRoot does not include the filename (gleaned from other answers).
– YetAnotherRandomUser
Jun 21 '17 at 11:26
This command includes the filename of the script, which threw me off until I realized that. When you are wanting the path, you probably don't want the script name in there too. At least, I can't think of a reason you would want that. $PSScriptRoot does not include the filename (gleaned from other answers).
– YetAnotherRandomUser
Jun 21 '17 at 11:26
1
1
Split-Path -Parent $PSCommandPath
did the job, thank you– Yoraco Gonzales
Sep 8 '18 at 18:41
Split-Path -Parent $PSCommandPath
did the job, thank you– Yoraco Gonzales
Sep 8 '18 at 18:41
add a comment |
Maybe i'm missing something here... but if you want the present working directory you can just use this: (Get-Location).Path
for a string, or Get-Location
for an object.
Unless you're referring to something like this, which I understand after reading the question again.
function Get-Script-Directory
{
$scriptInvocation = (Get-Variable MyInvocation -Scope 1).Value
return Split-Path $scriptInvocation.MyCommand.Path
}
15
This gets the current location where the user is running the script. Not the location of the script file itself.
– Aaron Jensen
Mar 29 '11 at 0:07
2
function Get-Script-Directory { $scriptInvocation = (Get-Variable MyInvocation -Scope 1).Value return Split-Path $scriptInvocation.MyCommand.Path } $hello = "hello" Write-Host (Get-Script-Directory) Write-Host $hello
Save that and run it from a different directory. You'll show the path to the script.
– Sean C.
Mar 29 '11 at 0:25
That's a good function, and does what I need, but how do I share it and use it in all my scripts? It's a chicken and egg problem: I'd like to use a function to find out my current location, but I need my location to load the function.
– Aaron Jensen
Mar 29 '11 at 2:26
2
NOTE: The invokation of this function must be at the top level of your script, if it is nested within another function, then you have to change the "-Scope" parameter to designate how deep in the call stack you are.
– kenny
Jun 1 '15 at 21:57
add a comment |
Maybe i'm missing something here... but if you want the present working directory you can just use this: (Get-Location).Path
for a string, or Get-Location
for an object.
Unless you're referring to something like this, which I understand after reading the question again.
function Get-Script-Directory
{
$scriptInvocation = (Get-Variable MyInvocation -Scope 1).Value
return Split-Path $scriptInvocation.MyCommand.Path
}
15
This gets the current location where the user is running the script. Not the location of the script file itself.
– Aaron Jensen
Mar 29 '11 at 0:07
2
function Get-Script-Directory { $scriptInvocation = (Get-Variable MyInvocation -Scope 1).Value return Split-Path $scriptInvocation.MyCommand.Path } $hello = "hello" Write-Host (Get-Script-Directory) Write-Host $hello
Save that and run it from a different directory. You'll show the path to the script.
– Sean C.
Mar 29 '11 at 0:25
That's a good function, and does what I need, but how do I share it and use it in all my scripts? It's a chicken and egg problem: I'd like to use a function to find out my current location, but I need my location to load the function.
– Aaron Jensen
Mar 29 '11 at 2:26
2
NOTE: The invokation of this function must be at the top level of your script, if it is nested within another function, then you have to change the "-Scope" parameter to designate how deep in the call stack you are.
– kenny
Jun 1 '15 at 21:57
add a comment |
Maybe i'm missing something here... but if you want the present working directory you can just use this: (Get-Location).Path
for a string, or Get-Location
for an object.
Unless you're referring to something like this, which I understand after reading the question again.
function Get-Script-Directory
{
$scriptInvocation = (Get-Variable MyInvocation -Scope 1).Value
return Split-Path $scriptInvocation.MyCommand.Path
}
Maybe i'm missing something here... but if you want the present working directory you can just use this: (Get-Location).Path
for a string, or Get-Location
for an object.
Unless you're referring to something like this, which I understand after reading the question again.
function Get-Script-Directory
{
$scriptInvocation = (Get-Variable MyInvocation -Scope 1).Value
return Split-Path $scriptInvocation.MyCommand.Path
}
edited Mar 29 '11 at 0:06
answered Mar 28 '11 at 23:58
Sean C.Sean C.
35215
35215
15
This gets the current location where the user is running the script. Not the location of the script file itself.
– Aaron Jensen
Mar 29 '11 at 0:07
2
function Get-Script-Directory { $scriptInvocation = (Get-Variable MyInvocation -Scope 1).Value return Split-Path $scriptInvocation.MyCommand.Path } $hello = "hello" Write-Host (Get-Script-Directory) Write-Host $hello
Save that and run it from a different directory. You'll show the path to the script.
– Sean C.
Mar 29 '11 at 0:25
That's a good function, and does what I need, but how do I share it and use it in all my scripts? It's a chicken and egg problem: I'd like to use a function to find out my current location, but I need my location to load the function.
– Aaron Jensen
Mar 29 '11 at 2:26
2
NOTE: The invokation of this function must be at the top level of your script, if it is nested within another function, then you have to change the "-Scope" parameter to designate how deep in the call stack you are.
– kenny
Jun 1 '15 at 21:57
add a comment |
15
This gets the current location where the user is running the script. Not the location of the script file itself.
– Aaron Jensen
Mar 29 '11 at 0:07
2
function Get-Script-Directory { $scriptInvocation = (Get-Variable MyInvocation -Scope 1).Value return Split-Path $scriptInvocation.MyCommand.Path } $hello = "hello" Write-Host (Get-Script-Directory) Write-Host $hello
Save that and run it from a different directory. You'll show the path to the script.
– Sean C.
Mar 29 '11 at 0:25
That's a good function, and does what I need, but how do I share it and use it in all my scripts? It's a chicken and egg problem: I'd like to use a function to find out my current location, but I need my location to load the function.
– Aaron Jensen
Mar 29 '11 at 2:26
2
NOTE: The invokation of this function must be at the top level of your script, if it is nested within another function, then you have to change the "-Scope" parameter to designate how deep in the call stack you are.
– kenny
Jun 1 '15 at 21:57
15
15
This gets the current location where the user is running the script. Not the location of the script file itself.
– Aaron Jensen
Mar 29 '11 at 0:07
This gets the current location where the user is running the script. Not the location of the script file itself.
– Aaron Jensen
Mar 29 '11 at 0:07
2
2
function Get-Script-Directory { $scriptInvocation = (Get-Variable MyInvocation -Scope 1).Value return Split-Path $scriptInvocation.MyCommand.Path } $hello = "hello" Write-Host (Get-Script-Directory) Write-Host $hello
Save that and run it from a different directory. You'll show the path to the script.– Sean C.
Mar 29 '11 at 0:25
function Get-Script-Directory { $scriptInvocation = (Get-Variable MyInvocation -Scope 1).Value return Split-Path $scriptInvocation.MyCommand.Path } $hello = "hello" Write-Host (Get-Script-Directory) Write-Host $hello
Save that and run it from a different directory. You'll show the path to the script.– Sean C.
Mar 29 '11 at 0:25
That's a good function, and does what I need, but how do I share it and use it in all my scripts? It's a chicken and egg problem: I'd like to use a function to find out my current location, but I need my location to load the function.
– Aaron Jensen
Mar 29 '11 at 2:26
That's a good function, and does what I need, but how do I share it and use it in all my scripts? It's a chicken and egg problem: I'd like to use a function to find out my current location, but I need my location to load the function.
– Aaron Jensen
Mar 29 '11 at 2:26
2
2
NOTE: The invokation of this function must be at the top level of your script, if it is nested within another function, then you have to change the "-Scope" parameter to designate how deep in the call stack you are.
– kenny
Jun 1 '15 at 21:57
NOTE: The invokation of this function must be at the top level of your script, if it is nested within another function, then you have to change the "-Scope" parameter to designate how deep in the call stack you are.
– kenny
Jun 1 '15 at 21:57
add a comment |
For Powershell 3+
function Get-ScriptDirectory {
if ($psise) {Split-Path $psise.CurrentFile.FullPath}
else {$global:PSScriptRoot}
}
I've placed this function in my profile. Works in ISE using F8/Run Selection too.
1
Nice to see a solution that also works in ISE
– KyleMit
Nov 1 '18 at 17:27
add a comment |
For Powershell 3+
function Get-ScriptDirectory {
if ($psise) {Split-Path $psise.CurrentFile.FullPath}
else {$global:PSScriptRoot}
}
I've placed this function in my profile. Works in ISE using F8/Run Selection too.
1
Nice to see a solution that also works in ISE
– KyleMit
Nov 1 '18 at 17:27
add a comment |
For Powershell 3+
function Get-ScriptDirectory {
if ($psise) {Split-Path $psise.CurrentFile.FullPath}
else {$global:PSScriptRoot}
}
I've placed this function in my profile. Works in ISE using F8/Run Selection too.
For Powershell 3+
function Get-ScriptDirectory {
if ($psise) {Split-Path $psise.CurrentFile.FullPath}
else {$global:PSScriptRoot}
}
I've placed this function in my profile. Works in ISE using F8/Run Selection too.
edited Jan 27 '17 at 9:32
answered Jan 20 '17 at 15:05
nickkzlnickkzl
10114
10114
1
Nice to see a solution that also works in ISE
– KyleMit
Nov 1 '18 at 17:27
add a comment |
1
Nice to see a solution that also works in ISE
– KyleMit
Nov 1 '18 at 17:27
1
1
Nice to see a solution that also works in ISE
– KyleMit
Nov 1 '18 at 17:27
Nice to see a solution that also works in ISE
– KyleMit
Nov 1 '18 at 17:27
add a comment |
Very similar to already posted answers, but piping seems more PS-like.
$PSCommandPath | Split-Path -Parent
add a comment |
Very similar to already posted answers, but piping seems more PS-like.
$PSCommandPath | Split-Path -Parent
add a comment |
Very similar to already posted answers, but piping seems more PS-like.
$PSCommandPath | Split-Path -Parent
Very similar to already posted answers, but piping seems more PS-like.
$PSCommandPath | Split-Path -Parent
answered Oct 13 '16 at 20:45
CPARCPAR
9114
9114
add a comment |
add a comment |
I use the automatic variable $ExecutionContext, it works from PowerShell 2 and later.
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('.')
$ExecutionContext
Contains an EngineIntrinsics object that represents the
execution context of the Windows PowerShell host. You can
use this variable to find the execution objects that are
available to cmdlets.
1
This is the only one it worked for me, trying to feed powershell from STDIN.
– Sebastian
Apr 15 '17 at 16:45
This solution also works properly when you are in a UNC path context.
– user2030503
Feb 12 '18 at 7:50
1
This is apparently picking up the working directory - rather than where the script is located ?
– monojohnny
Nov 9 '18 at 12:05
@monojohnny Yes this is basically the current working directory and will not work when calling a script from another location.
– marsze
Jan 24 at 11:26
add a comment |
I use the automatic variable $ExecutionContext, it works from PowerShell 2 and later.
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('.')
$ExecutionContext
Contains an EngineIntrinsics object that represents the
execution context of the Windows PowerShell host. You can
use this variable to find the execution objects that are
available to cmdlets.
1
This is the only one it worked for me, trying to feed powershell from STDIN.
– Sebastian
Apr 15 '17 at 16:45
This solution also works properly when you are in a UNC path context.
– user2030503
Feb 12 '18 at 7:50
1
This is apparently picking up the working directory - rather than where the script is located ?
– monojohnny
Nov 9 '18 at 12:05
@monojohnny Yes this is basically the current working directory and will not work when calling a script from another location.
– marsze
Jan 24 at 11:26
add a comment |
I use the automatic variable $ExecutionContext, it works from PowerShell 2 and later.
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('.')
$ExecutionContext
Contains an EngineIntrinsics object that represents the
execution context of the Windows PowerShell host. You can
use this variable to find the execution objects that are
available to cmdlets.
I use the automatic variable $ExecutionContext, it works from PowerShell 2 and later.
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('.')
$ExecutionContext
Contains an EngineIntrinsics object that represents the
execution context of the Windows PowerShell host. You can
use this variable to find the execution objects that are
available to cmdlets.
edited Mar 11 '18 at 4:49
Dave Wood
10.4k14155
10.4k14155
answered Feb 25 '16 at 9:07
ViggosViggos
10916
10916
1
This is the only one it worked for me, trying to feed powershell from STDIN.
– Sebastian
Apr 15 '17 at 16:45
This solution also works properly when you are in a UNC path context.
– user2030503
Feb 12 '18 at 7:50
1
This is apparently picking up the working directory - rather than where the script is located ?
– monojohnny
Nov 9 '18 at 12:05
@monojohnny Yes this is basically the current working directory and will not work when calling a script from another location.
– marsze
Jan 24 at 11:26
add a comment |
1
This is the only one it worked for me, trying to feed powershell from STDIN.
– Sebastian
Apr 15 '17 at 16:45
This solution also works properly when you are in a UNC path context.
– user2030503
Feb 12 '18 at 7:50
1
This is apparently picking up the working directory - rather than where the script is located ?
– monojohnny
Nov 9 '18 at 12:05
@monojohnny Yes this is basically the current working directory and will not work when calling a script from another location.
– marsze
Jan 24 at 11:26
1
1
This is the only one it worked for me, trying to feed powershell from STDIN.
– Sebastian
Apr 15 '17 at 16:45
This is the only one it worked for me, trying to feed powershell from STDIN.
– Sebastian
Apr 15 '17 at 16:45
This solution also works properly when you are in a UNC path context.
– user2030503
Feb 12 '18 at 7:50
This solution also works properly when you are in a UNC path context.
– user2030503
Feb 12 '18 at 7:50
1
1
This is apparently picking up the working directory - rather than where the script is located ?
– monojohnny
Nov 9 '18 at 12:05
This is apparently picking up the working directory - rather than where the script is located ?
– monojohnny
Nov 9 '18 at 12:05
@monojohnny Yes this is basically the current working directory and will not work when calling a script from another location.
– marsze
Jan 24 at 11:26
@monojohnny Yes this is basically the current working directory and will not work when calling a script from another location.
– marsze
Jan 24 at 11:26
add a comment |
I needed to know the script name and where it is executing from.
Prefixing "$global:" to the MyInvocation structure returns the full path and script name when called from both the main script, and the main line of an imported .PSM1 library file. It also works from within a function in an imported library.
After much fiddling around, I settled on using $global:MyInvocation.InvocationName.
It works reliably with CMD launch, Run With Powershell, and the ISE.
Both local and UNC launches return the correct path.
4
Split-Path -Path $($global:MyInvocation.MyCommand.Path) worked perfect thanks. The other solutions returned the path of the calling application.
– C0r3yh
May 6 '14 at 21:24
1
Trivial note: in ISE calling this function using F8/Run Selection will trigger aParameterArgumentValidationErrorNullNotAllowed
exception.
– weir
Oct 19 '15 at 14:15
add a comment |
I needed to know the script name and where it is executing from.
Prefixing "$global:" to the MyInvocation structure returns the full path and script name when called from both the main script, and the main line of an imported .PSM1 library file. It also works from within a function in an imported library.
After much fiddling around, I settled on using $global:MyInvocation.InvocationName.
It works reliably with CMD launch, Run With Powershell, and the ISE.
Both local and UNC launches return the correct path.
4
Split-Path -Path $($global:MyInvocation.MyCommand.Path) worked perfect thanks. The other solutions returned the path of the calling application.
– C0r3yh
May 6 '14 at 21:24
1
Trivial note: in ISE calling this function using F8/Run Selection will trigger aParameterArgumentValidationErrorNullNotAllowed
exception.
– weir
Oct 19 '15 at 14:15
add a comment |
I needed to know the script name and where it is executing from.
Prefixing "$global:" to the MyInvocation structure returns the full path and script name when called from both the main script, and the main line of an imported .PSM1 library file. It also works from within a function in an imported library.
After much fiddling around, I settled on using $global:MyInvocation.InvocationName.
It works reliably with CMD launch, Run With Powershell, and the ISE.
Both local and UNC launches return the correct path.
I needed to know the script name and where it is executing from.
Prefixing "$global:" to the MyInvocation structure returns the full path and script name when called from both the main script, and the main line of an imported .PSM1 library file. It also works from within a function in an imported library.
After much fiddling around, I settled on using $global:MyInvocation.InvocationName.
It works reliably with CMD launch, Run With Powershell, and the ISE.
Both local and UNC launches return the correct path.
edited May 12 '14 at 22:21
answered Apr 15 '14 at 0:57
Bruce GavinBruce Gavin
19926
19926
4
Split-Path -Path $($global:MyInvocation.MyCommand.Path) worked perfect thanks. The other solutions returned the path of the calling application.
– C0r3yh
May 6 '14 at 21:24
1
Trivial note: in ISE calling this function using F8/Run Selection will trigger aParameterArgumentValidationErrorNullNotAllowed
exception.
– weir
Oct 19 '15 at 14:15
add a comment |
4
Split-Path -Path $($global:MyInvocation.MyCommand.Path) worked perfect thanks. The other solutions returned the path of the calling application.
– C0r3yh
May 6 '14 at 21:24
1
Trivial note: in ISE calling this function using F8/Run Selection will trigger aParameterArgumentValidationErrorNullNotAllowed
exception.
– weir
Oct 19 '15 at 14:15
4
4
Split-Path -Path $($global:MyInvocation.MyCommand.Path) worked perfect thanks. The other solutions returned the path of the calling application.
– C0r3yh
May 6 '14 at 21:24
Split-Path -Path $($global:MyInvocation.MyCommand.Path) worked perfect thanks. The other solutions returned the path of the calling application.
– C0r3yh
May 6 '14 at 21:24
1
1
Trivial note: in ISE calling this function using F8/Run Selection will trigger a
ParameterArgumentValidationErrorNullNotAllowed
exception.– weir
Oct 19 '15 at 14:15
Trivial note: in ISE calling this function using F8/Run Selection will trigger a
ParameterArgumentValidationErrorNullNotAllowed
exception.– weir
Oct 19 '15 at 14:15
add a comment |
Took me a while to develop something that took the accepted answer and turned it into a robust function.
Not sure about others but I work in an environment with machines on both PowerShell version 2 and 3 so I needed to handle both. The following function offers a graceful fallback:
Function Get-PSScriptRoot
{
$ScriptRoot = ""
Try
{
$ScriptRoot = Get-Variable -Name PSScriptRoot -ValueOnly -ErrorAction Stop
}
Catch
{
$ScriptRoot = Split-Path $script:MyInvocation.MyCommand.Path
}
Write-Output $ScriptRoot
}
It also means that the function refers to the Script scope rather than the parent's scope as outlined by Michael Sorens in his blog
Thanks! The "$script:" was what I needed to get this to work in Windows PowerShell ISE.
– Kirk Liemohn
Jan 11 '17 at 20:20
add a comment |
Took me a while to develop something that took the accepted answer and turned it into a robust function.
Not sure about others but I work in an environment with machines on both PowerShell version 2 and 3 so I needed to handle both. The following function offers a graceful fallback:
Function Get-PSScriptRoot
{
$ScriptRoot = ""
Try
{
$ScriptRoot = Get-Variable -Name PSScriptRoot -ValueOnly -ErrorAction Stop
}
Catch
{
$ScriptRoot = Split-Path $script:MyInvocation.MyCommand.Path
}
Write-Output $ScriptRoot
}
It also means that the function refers to the Script scope rather than the parent's scope as outlined by Michael Sorens in his blog
Thanks! The "$script:" was what I needed to get this to work in Windows PowerShell ISE.
– Kirk Liemohn
Jan 11 '17 at 20:20
add a comment |
Took me a while to develop something that took the accepted answer and turned it into a robust function.
Not sure about others but I work in an environment with machines on both PowerShell version 2 and 3 so I needed to handle both. The following function offers a graceful fallback:
Function Get-PSScriptRoot
{
$ScriptRoot = ""
Try
{
$ScriptRoot = Get-Variable -Name PSScriptRoot -ValueOnly -ErrorAction Stop
}
Catch
{
$ScriptRoot = Split-Path $script:MyInvocation.MyCommand.Path
}
Write-Output $ScriptRoot
}
It also means that the function refers to the Script scope rather than the parent's scope as outlined by Michael Sorens in his blog
Took me a while to develop something that took the accepted answer and turned it into a robust function.
Not sure about others but I work in an environment with machines on both PowerShell version 2 and 3 so I needed to handle both. The following function offers a graceful fallback:
Function Get-PSScriptRoot
{
$ScriptRoot = ""
Try
{
$ScriptRoot = Get-Variable -Name PSScriptRoot -ValueOnly -ErrorAction Stop
}
Catch
{
$ScriptRoot = Split-Path $script:MyInvocation.MyCommand.Path
}
Write-Output $ScriptRoot
}
It also means that the function refers to the Script scope rather than the parent's scope as outlined by Michael Sorens in his blog
answered Apr 9 '16 at 13:21
BrunoBruno
4,89811734
4,89811734
Thanks! The "$script:" was what I needed to get this to work in Windows PowerShell ISE.
– Kirk Liemohn
Jan 11 '17 at 20:20
add a comment |
Thanks! The "$script:" was what I needed to get this to work in Windows PowerShell ISE.
– Kirk Liemohn
Jan 11 '17 at 20:20
Thanks! The "$script:" was what I needed to get this to work in Windows PowerShell ISE.
– Kirk Liemohn
Jan 11 '17 at 20:20
Thanks! The "$script:" was what I needed to get this to work in Windows PowerShell ISE.
– Kirk Liemohn
Jan 11 '17 at 20:20
add a comment |
You might also consider split-path -parent $psISE.CurrentFile.Fullpath
if any of the other methods fail. In particular, if you run a file to load a bunch of functions and then execute those functions with-in the ISE shell (or if you run-selected), it seems the Get-Script-Directory
function as above doesn't work.
3
$PSCommandPath
will work in the ISE as long as you save the script first and execute the whole file. Otherwise, you're not actually executing a script; you're just "pasting" commands into the shell.
– Zenexer
Jul 25 '13 at 4:02
@Zenexer I think that was my goal at the time. Although if my goal didn't match up with the original one, this might not be too helpful except to the occasional Googlers...
– user1499731
Jan 16 '15 at 16:36
add a comment |
You might also consider split-path -parent $psISE.CurrentFile.Fullpath
if any of the other methods fail. In particular, if you run a file to load a bunch of functions and then execute those functions with-in the ISE shell (or if you run-selected), it seems the Get-Script-Directory
function as above doesn't work.
3
$PSCommandPath
will work in the ISE as long as you save the script first and execute the whole file. Otherwise, you're not actually executing a script; you're just "pasting" commands into the shell.
– Zenexer
Jul 25 '13 at 4:02
@Zenexer I think that was my goal at the time. Although if my goal didn't match up with the original one, this might not be too helpful except to the occasional Googlers...
– user1499731
Jan 16 '15 at 16:36
add a comment |
You might also consider split-path -parent $psISE.CurrentFile.Fullpath
if any of the other methods fail. In particular, if you run a file to load a bunch of functions and then execute those functions with-in the ISE shell (or if you run-selected), it seems the Get-Script-Directory
function as above doesn't work.
You might also consider split-path -parent $psISE.CurrentFile.Fullpath
if any of the other methods fail. In particular, if you run a file to load a bunch of functions and then execute those functions with-in the ISE shell (or if you run-selected), it seems the Get-Script-Directory
function as above doesn't work.
edited Jan 29 '13 at 21:44
Rudi Visser
17.2k45086
17.2k45086
answered Jan 29 '13 at 21:28
user2023266user2023266
392
392
3
$PSCommandPath
will work in the ISE as long as you save the script first and execute the whole file. Otherwise, you're not actually executing a script; you're just "pasting" commands into the shell.
– Zenexer
Jul 25 '13 at 4:02
@Zenexer I think that was my goal at the time. Although if my goal didn't match up with the original one, this might not be too helpful except to the occasional Googlers...
– user1499731
Jan 16 '15 at 16:36
add a comment |
3
$PSCommandPath
will work in the ISE as long as you save the script first and execute the whole file. Otherwise, you're not actually executing a script; you're just "pasting" commands into the shell.
– Zenexer
Jul 25 '13 at 4:02
@Zenexer I think that was my goal at the time. Although if my goal didn't match up with the original one, this might not be too helpful except to the occasional Googlers...
– user1499731
Jan 16 '15 at 16:36
3
3
$PSCommandPath
will work in the ISE as long as you save the script first and execute the whole file. Otherwise, you're not actually executing a script; you're just "pasting" commands into the shell.– Zenexer
Jul 25 '13 at 4:02
$PSCommandPath
will work in the ISE as long as you save the script first and execute the whole file. Otherwise, you're not actually executing a script; you're just "pasting" commands into the shell.– Zenexer
Jul 25 '13 at 4:02
@Zenexer I think that was my goal at the time. Although if my goal didn't match up with the original one, this might not be too helpful except to the occasional Googlers...
– user1499731
Jan 16 '15 at 16:36
@Zenexer I think that was my goal at the time. Although if my goal didn't match up with the original one, this might not be too helpful except to the occasional Googlers...
– user1499731
Jan 16 '15 at 16:36
add a comment |
Found that the older solutions posted here didn't work for me on PowerShell V5. I came up with this:
try {
$scriptPath = $PSScriptRoot
if (!$scriptPath)
{
if ($psISE)
{
$scriptPath = Split-Path -Parent -Path $psISE.CurrentFile.FullPath
} else {
Write-Host -ForegroundColor Red "Cannot resolve script file's path"
exit 1
}
}
} catch {
Write-Host -ForegroundColor Red "Caught Exception: $($Error[0].Exception.Message)"
exit 2
}
Write-Host "Path: $scriptPath"
HTH
add a comment |
Found that the older solutions posted here didn't work for me on PowerShell V5. I came up with this:
try {
$scriptPath = $PSScriptRoot
if (!$scriptPath)
{
if ($psISE)
{
$scriptPath = Split-Path -Parent -Path $psISE.CurrentFile.FullPath
} else {
Write-Host -ForegroundColor Red "Cannot resolve script file's path"
exit 1
}
}
} catch {
Write-Host -ForegroundColor Red "Caught Exception: $($Error[0].Exception.Message)"
exit 2
}
Write-Host "Path: $scriptPath"
HTH
add a comment |
Found that the older solutions posted here didn't work for me on PowerShell V5. I came up with this:
try {
$scriptPath = $PSScriptRoot
if (!$scriptPath)
{
if ($psISE)
{
$scriptPath = Split-Path -Parent -Path $psISE.CurrentFile.FullPath
} else {
Write-Host -ForegroundColor Red "Cannot resolve script file's path"
exit 1
}
}
} catch {
Write-Host -ForegroundColor Red "Caught Exception: $($Error[0].Exception.Message)"
exit 2
}
Write-Host "Path: $scriptPath"
HTH
Found that the older solutions posted here didn't work for me on PowerShell V5. I came up with this:
try {
$scriptPath = $PSScriptRoot
if (!$scriptPath)
{
if ($psISE)
{
$scriptPath = Split-Path -Parent -Path $psISE.CurrentFile.FullPath
} else {
Write-Host -ForegroundColor Red "Cannot resolve script file's path"
exit 1
}
}
} catch {
Write-Host -ForegroundColor Red "Caught Exception: $($Error[0].Exception.Message)"
exit 2
}
Write-Host "Path: $scriptPath"
HTH
answered Apr 3 at 19:18
QuantiumQuantium
1,3861113
1,3861113
add a comment |
add a comment |
function func1()
{
$inv = (Get-Variable MyInvocation -Scope 1).Value
#$inv.MyCommand | Format-List *
$Path1 = Split-Path $inv.scriptname
Write-Host $Path1
}
function Main()
{
func1
}
Main
add a comment |
function func1()
{
$inv = (Get-Variable MyInvocation -Scope 1).Value
#$inv.MyCommand | Format-List *
$Path1 = Split-Path $inv.scriptname
Write-Host $Path1
}
function Main()
{
func1
}
Main
add a comment |
function func1()
{
$inv = (Get-Variable MyInvocation -Scope 1).Value
#$inv.MyCommand | Format-List *
$Path1 = Split-Path $inv.scriptname
Write-Host $Path1
}
function Main()
{
func1
}
Main
function func1()
{
$inv = (Get-Variable MyInvocation -Scope 1).Value
#$inv.MyCommand | Format-List *
$Path1 = Split-Path $inv.scriptname
Write-Host $Path1
}
function Main()
{
func1
}
Main
edited Apr 28 '15 at 7:47
Milen
6,97572650
6,97572650
answered Apr 28 '15 at 6:50
RaviRavi
1
1
add a comment |
add a comment |
protected by Community♦ Mar 15 '17 at 0:36
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
1
possible duplicate of How can i get the file system location of a powershell script?
– JohnC
Apr 14 '14 at 11:09