powershell testing a variable that hasnt being assign yet

Test-Path variable:\var should do what you want, I guess.


Any unassigned variable will have a value of null, not a data type of null. So, just do this:

If ($ProgramName -ne $null)

...that will return TRUE if it's been assigned to a non-null value.

An even easier check to make is

IF($ProgramName)

Which will check if that is $null or not, though the logic is reversed, so you could use

IF(!$ProgramName)

Edit:

Ruffin raises a good point about strictmode in comments. This method will work as well:

Test-Path variable:ProgramName or Test-Path variable:global:ProgramName if it's explicitly global scoped, for instance. This will return $true or $false depending on if the variable exists.