3

I like to make a lot of changes to my $profile file. After saving a change, I would like to source the file using an "sop" alias. Here's how I hope to accomplish that:

set-alias sop Source-Profile function Source-Profile { . $profile } 

This function does not throw any errors, so it appears to work. However, when I do make changes to my $profile file, this function doesn’t appear to to anything. I still have to type . $profile from the PS prompt to re-evaluate that file. And that's ok, but it is a minor pain. I would rather use an alias to source that file.

What am I missing here?

2 Answers 2

3

Dot-sourcing a file runs it in the current scope (rather than its own scope). To source a file in a function and get the definitions in the file to be included in the calling scope, you need to dot-source the function. Try this:

. sop 
1
  • Awesome, this works! Too bad though that you can't do this without the dot. This sort of thing works in BASH and such. Commented Mar 5, 2013 at 16:33
1

13 year old bump for anyone that might find this later on:

I'm pretty sure Microsoft added to PowerShell functionality at some point, either way from now on you can use a 'global' scope for your sourcing function so you don't need to deal with putting an annoying dot when sourcing your profile.

function global:Source-Profile{ . $PROFILE } 

Works now.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.