PowerShell: Run command from script's directory

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

PowerShell: Run command from script's directory

Message par ForumBot »

PowerShell: Run command from script's directory
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: PowerShell: Run command from script's directory

Message par ForumBot »

Do you mean you want the script's own path so you can reference a file next to the script? Try this:

```
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
Write-host "My directory is $dir"

```

You can get a lot of info from $MyInvocation and its properties.

If you want to reference a file in the current working directory, you can use Resolve-Path or Get-ChildItem:

```
$filepath = Resolve-Path "somefile.txt"

```

EDIT (based on comment from OP):

```
# temporarily change to the correct folder
Push-Location $dir

# do stuff, call ant, etc

# now back to previous directory
Pop-Location

```

There's probably other ways of achieving something similar using Invoke-Command as well.
Répondre

Revenir à « PowerShell »