3

I am working on a PowerShell script intended to be run in Package Manager Console. I am able to add a script at the location returned by $profile in Package Manager Console, but I am not able to get the script to pick up changes without opening a fresh instance of Visual Studio.

According to this article, you should be able to reload the script by typing & $profile:

When you make changes to this user profile while Visual Studio is open Visual Studio will not detect any changes. You can type & $profile in the Package Manager Console to reload the profile.

I can use & $profile to verify my script does not have syntax errors, but it does not load the changes into the console session, which requires me to open a fresh Visual Studio session every time I make a script change.

For example, if I set my profile script to:

function foo{
  Write-Output "bar"
}

and then open Visual Studio and Package Manager Console, I can type "foo" at the prompt and get back "bar". If I change "function" to "functionX" and run & $profile, I will get a ParserError in the console. But if I change the script body to Write-Output "baz" and then run & $profile at the console prompt, and I then type foo, I will still get back "bar". The code change is only picked up in a fresh instance of Visual Studio.

Is there a setting in Visual Studio that changes this behavior or a way to force the new script to be loaded?

2
  • I believe all you need to do is "dot source" $profile so it runs in that scope again: . $profile Commented Jun 27, 2017 at 14:04
  • This did the trick! Please add as an answer so I can accept. Commented Jun 27, 2017 at 15:10

1 Answer 1

2

When a script is executed without dot sourcing the variables, functions, etc. defined in that script are not added to the parent scope. By adding a period in front of the command will dot source the script and therefore add things in that parent scope.

In your specific case the command will be:

. $profile
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.