Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emulating 'source' command in powershell

Tags:

powershell

I am trying to emulate bash's source command in powershell. The intent is to make any change to my microsoft.powershell_profile.psl and source it into the existing instance of powershell.

The following command works in command-line

$profile_content = [string]::join([environment]::newline,(get-content $profile))
invoke-expression $profile_content

All is good; I put the same into microsoft.powershell_profile.psl and it does not work.

function source{
        $profile_content = [string]::join([environment]::newline,(get-content $args[0]))
        invoke-expression $profile_content
}

Am I overlooking something?

like image 761
Antony Thomas Avatar asked May 09 '26 01:05

Antony Thomas


2 Answers

What you want is already built into PowerShell:

. C:\path\to\some.ps1

See about_Operators:

. Dot sourcing operator
Runs a script in the current scope so that any functions, aliases, and variables that the script creates are added to the current scope.

. c:\scripts.sample.ps1
like image 180
Ansgar Wiechers Avatar answered May 11 '26 15:05

Ansgar Wiechers


Following should be enough:

function source { . $args }
like image 45
sheerun Avatar answered May 11 '26 14:05

sheerun



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!