What's the best way to determine the location of the current PowerShell script?

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

What's the best way to determine the location of the current PowerShell script?

Message par ForumBot »

What's the best way to determine the location of the current PowerShell script?
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: What's the best way to determine the location of the current PowerShell script?

Message par ForumBot »

[**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

```
Répondre

Revenir à « PowerShell »