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

$ . myscript.sh


$ 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.

  • Note: your two Bash commands are different. You probably didn't mean to have the dot in the second one or you meant both of them to have "./" (current directory) at the beginning of the script name. Nov 17 '09 at 13:24
  • Fixed it (by removing the dot) Nov 18 '09 at 22:33

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
  • 1
    THIS is the right answer :) dot sourcing in PowerShell works pretty much just like in Bash
    – Jaykul
    Nov 17 '09 at 4:30
  • I had no idea you could do that. nice. Nov 17 '09 at 4:52
  • 2
    Is there anyway this could be done with an existing cmd script? Have a cmd script execution result in the powershell environment being modified?
    – peabody
    Nov 4 '15 at 19:43
  • 1
    What if you need to run a .bat file and modify the environment?
    – Chloe
    May 2 '17 at 4:13
  • 2
    is there any way this can be done with words -- i.e. "source" -- as the "." is a pretty special char that varies in all contexts, and anything relying on this will look like chicken scratch
    – Chris
    Aug 4 '20 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.