How To Check Variable Exists in PowerShell Session

Hello!

Recently I needed to check that a variable exists in the PowerShell session currently running. This is actually far easier than it sounds. So here is a simple demo for how it works. The magic here is the “Test-Path variable:my_variable” on lines 4. It tests that a variable of that name exists. If it does, great, let’s print out the value. If not, let’s alert that it doesn’t. The second example of this on line 11 will do exactly that.

This is a particularly useful way of updating config files in memory, which is useful when you have username/passwords and don’t want them stored on boxes where they shouldn’t be. A full demo of this will be provided in the near future.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *