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

Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

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

Message par Clippy »

What's the best way to determine the location of the current PowerShell script?
Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

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

Message par Clippy »

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