Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
$my_variable = "this is the value of my_variable"
if (Test-Path variable:my_variable) {
Write-Host $my_variable -ForegroundColor Magenta -BackgroundColor Yellow
}
else {
Write-Host 'Variable $my_variable does not exist.' -ForegroundColor Yellow -BackgroundColor Magenta
}
#note: my_non_existent_variable doesn't exist, so will go to else statement
if (Test-Path variable:my_non_existent_variable) {
Write-Host $my_non_existent_variable -ForegroundColor Magenta -BackgroundColor Yellow
}
else {
Write-Host 'Variable $my_non_existent_variable does not exist.' -ForegroundColor Yellow -BackgroundColor Magenta
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment