Popular Posts

Wednesday, December 14, 2016

PowerShell System Variables / "Cannot override variable" error

I was working on a PowerShell script at work where I came across this weird issue. I kept getting "Cannot override variable" error when I try to run my script. After hours of trials and some help of StackOverflow, i learnt that one of the variables I had used is actually a system variable that cannot be overridden. In Fact, I had used $error as a variable to store an error message, a string, which was causing my script to fail.

To list down PowerShell system variables, use Get-Childitem env:

$Error is not really listed here, unless there is actual error created. But if you do a write-host or return from command prompt and type $erro or less, a Tab key input will complete the command. And ofcouse you can write to cmd or return the value, which in most cases is a null.