Call PowerShell script PS1 from another PS1 script inside Powershell ISE

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

Call PowerShell script PS1 from another PS1 script inside Powershell ISE

Message par ForumBot »

Call PowerShell script PS1 from another PS1 script inside Powershell ISE
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: Call PowerShell script PS1 from another PS1 script inside Powershell ISE

Message par ForumBot »

In order run a script in the same directory.

In **PowerShell 3.0** and later you can use the automatic variable `$PSScriptRoot`:

```
$PSScriptRoot/myScript1.ps1

```

In **PowerShell 1.0 and 2.0** you should use this specific property:

```
& "$(Split-Path $MyInvocation.MyCommand.Path)/myScript1.ps1"

```

The reason you should use that and not anything else can be illustrated with this example script.

```
## ScriptTest.ps1
Write-Host "InvocationName:" $MyInvocation.InvocationName
Write-Host "Path:" $MyInvocation.MyCommand.Path

```

Here are some results.

PS C:\Users\JasonAr> .\ScriptTest.ps1
InvocationName: .\ScriptTest.ps1
Path: C:\Users\JasonAr\ScriptTest.ps1

PS C:\Users\JasonAr> . .\ScriptTest.ps1
InvocationName: .
Path: C:\Users\JasonAr\ScriptTest.ps1

PS C:\Users\JasonAr> & ".\ScriptTest.ps1"
InvocationName: &
Path: C:\Users\JasonAr\ScriptTest.ps1

In **PowerShell 3.0** and later you can use the automatic variable `$PSScriptRoot`:

```
## ScriptTest.ps1
Write-Host "Script:" $PSCommandPath
Write-Host "Path:" $PSScriptRoot

```

PS C:\Users\jarcher> .\ScriptTest.ps1
Script: C:\Users\jarcher\ScriptTest.ps1
Path: C:\Users\jarcher
Répondre

Revenir à « PowerShell »