Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include relative files in PowerShell

I would like to include script files with such pseudo syntax:

Include '.\scripA.ps1' 

But the only thing I have found is some thing like this:

$thisScript = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent . ($thisScript + '.\scriptA.ps1') 

that is ugly.

Is there some nice way to include scripts with relative paths?

like image 632
alex2k8 Avatar asked Mar 26 '09 01:03

alex2k8


People also ask

What is LiteralPath in PowerShell?

-LiteralPath. Specifies the path to be resolved. The value of the LiteralPath parameter is used exactly as typed. No characters are interpreted as wildcard characters. If the path includes escape characters, enclose it in single quotation marks ( ' ).

How do I get full path in PowerShell?

Use the Get-ChildItem cmdlet in PowerShell to get the full path of the file in the current directory. Get-ChildItem returns one or more items from the specified location and using the file FullName property, it gets the full path of the file.

What does split path do in PowerShell?

The Split-Path cmdlet returns only the specified part of a path, such as the parent folder, a subfolder, or a file name. It can also get items that are referenced by the split path and tell whether the path is relative or absolute. You can use this cmdlet to get or submit only a selected part of a path.


1 Answers

You can utilize the $PSScriptRoot parameter like this:

. "$PSScriptRoot\script.ps1" 
like image 140
chrischu Avatar answered Oct 10 '22 13:10

chrischu