Thursday, December 30, 2010

Reinitializing your Powershell environment without closing and reopening

I’ve been running our builds with psake, and have found that I get errors if I try to run a build script twice in the same Powershell session.  I looked around a bit about how to reset your Powershell environment without closing and reopening a Powershell console window, but couldn’t find anything. 

Here’s what I came up with that works for me.

Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host

And now, the short version:

rv * -ea SilentlyContinue; rmo *; $error.Clear(); cls


psychophrenes said...

Quick and concise solution.

However, that doesn't work in all cases. For example, constant variables are not removed when using this solution.
New-Variable ConstantVar -Option constant -value "This is a contant"
In that case, Remove-Variable * fails.

Cindy Dy said...

You are an awesome blogger. This is one of the best blog I had visited so far. Hope to read more post from you in the future. Keep it up. God bless.