Equivalent of bash’s `source` command in Powershell

bashpowershell

To run a bash script line-by-line (so as to preserve environment variables, for instance), one does the following:

$ . myscript.sh

Or:

$ source myscript.sh

I have a PowerShell script that – among other things – sets the value of the prompt via function called prompt. Simply running this script (".\myscript.ps1") does not change the prompt. However, running function line-by-line as a command does.

So what I effectively need is something like the source command for PowerShell. Is it possible?

EDIT: Should this post be moved to Stackoverflow?

Clarification 1: Since I run this function only on a need basis (i.e., not for every powershell session and/or always), I do not want to store in my powershell startup profile.

Best Answer

You can "dot source" in PowerShell as well. You just need to make sure that you specify the full path. So, if the script you want to load is in the local directory you would do:

PS C:\>. .\myscript.ps1