Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dot sourcing a PowerShell script not ending with .ps1

From a PowerShell program, I can "dot source" another PowerShell program. i.e I can execute it as if it were written inside the first one.
Example:

Write-Host 'before'
. MyOtherProgram.ps1
Write-Host 'after'

MyOtherProgram in 'included' inside the main program, exactly as if its content had been copy/pasted.

The problem is: I can only dot source a filename finishing with .ps1
I can't with MyOtherProgram.lib or MyOtherProgram.whatever

Anyone have a method to dot source a PowerShell script not ending with .ps1 ?

like image 607
Gregory MOUSSAT Avatar asked Oct 29 '25 17:10

Gregory MOUSSAT


1 Answers

Another way would be using Invoke-Expression:

$code = Get-Content ./MyOtherProgram.lib | Out-String
Invoke-Expression $code

or shorter

Invoke-Expression "$(Get-Content ./MyOtherProgram.lib | Out-String)"
like image 72
Shay Levy Avatar answered Oct 31 '25 11:10

Shay Levy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!