What's the best way to check if the variable is not equal to null in PowerShell?
I have tried the below script
$site = Get-SPWeb -Site "http://mysite" if($site -ne null) { #MyCode }
But I get unexpected token null in expression or statement error
You are missing '$' sign before NULL, try to use $null
$
$null
If($site - nq $null) { #if site variable not equal Null}
Hope it helps
Besides @Imran answer, I would like to share another method to check if a variable is equal or not equal to null in PowerShell
#Metod 1 if($variable -ne $null) { } #Method 2 if($variable) { }
# Metod 1 if($variable -eq $null) { } #Method 2 if(!$variable) { }
1k questions
671 answers
451 comments
193k users
Related Activities
Categories
Most popular tags