What's the best way to determine the location of the current PowerShell script?
What's the best way to determine the location of the current PowerShell script?
What's the best way to determine the location of the current PowerShell script?
Re: What's the best way to determine the location of the current PowerShell script?
[**PowerShell 3+**](https://stackoverflow.com/a/5467533)
`# 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
```
`# 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
```