Comments on: Powershell: check variable for null https://www.thomasmaurer.ch/2010/07/powershell-check-variable-for-null/ Cloud and Datacenter Blog focusing on Microsoft Azure Thu, 22 Jun 2023 10:57:19 +0000 hourly 1 https://wordpress.org/?v=6.4.1 By: Ruszlán Gaszanov https://www.thomasmaurer.ch/2010/07/powershell-check-variable-for-null/#comment-892781 Thu, 22 Jun 2023 10:57:19 +0000 http://www.thomasmaurer.ch/?p=763#comment-892781 There is a very simple way to test is a PowerShell variable is defined and not null:

if ($var -is [object]) { ‘$var is defined and not null’ } else { ‘$var is undefined or not null’}

This is works because all valid PowerShell values other than $null are instances of the .NET System.Object class

]]>
By: Thomas Maurer https://www.thomasmaurer.ch/2010/07/powershell-check-variable-for-null/#comment-817275 Sun, 27 Sep 2020 09:16:56 +0000 http://www.thomasmaurer.ch/?p=763#comment-817275 In reply to Fernando.

You’re welcome :)

]]>
By: Fernando https://www.thomasmaurer.ch/2010/07/powershell-check-variable-for-null/#comment-816960 Fri, 25 Sep 2020 13:40:18 +0000 http://www.thomasmaurer.ch/?p=763#comment-816960 Thanks!

]]>
By: Marco https://www.thomasmaurer.ch/2010/07/powershell-check-variable-for-null/#comment-779302 Thu, 07 May 2020 10:09:45 +0000 http://www.thomasmaurer.ch/?p=763#comment-779302 does it work for Boolean variables?
I guess no!

]]>
By: antonio https://www.thomasmaurer.ch/2010/07/powershell-check-variable-for-null/#comment-725182 Mon, 25 Mar 2019 15:17:27 +0000 http://www.thomasmaurer.ch/?p=763#comment-725182 maybe if ($varibalename -eq $null) { Write-Host “variable is null” } does not work because of the typo?
$varibalename should be $variablename

]]>
By: PowerPete https://www.thomasmaurer.ch/2010/07/powershell-check-variable-for-null/#comment-700864 Tue, 27 Mar 2018 12:36:36 +0000 http://www.thomasmaurer.ch/?p=763#comment-700864 $foo = $null

# Confused intent of null check as type inference takes place
# this Returns $True and the output!
if(!$foo) {
Write-Host ‘This runs’
}

See: https://rencore.com/blog/powershell-null-comparison/

]]>
By: cividan https://www.thomasmaurer.ch/2010/07/powershell-check-variable-for-null/#comment-692269 Thu, 26 Oct 2017 19:03:18 +0000 http://www.thomasmaurer.ch/?p=763#comment-692269 Thanks alot this was very helpfull to me as my check for null were not working in my script.

]]>
By: rotem https://www.thomasmaurer.ch/2010/07/powershell-check-variable-for-null/#comment-687993 Sun, 20 Aug 2017 02:38:20 +0000 http://www.thomasmaurer.ch/?p=763#comment-687993 simply use this check statement after the match check

$result = $t -match “‘(?[a-zA-Z]+)”
if(-not $result)
{
continue
}

]]>
By: Ian https://www.thomasmaurer.ch/2010/07/powershell-check-variable-for-null/#comment-675706 Tue, 31 Jan 2017 14:22:17 +0000 http://www.thomasmaurer.ch/?p=763#comment-675706 Thanks for this, really useful to know, especially for a beginer.

]]>
By: Ano https://www.thomasmaurer.ch/2010/07/powershell-check-variable-for-null/#comment-668515 Thu, 17 Nov 2016 00:06:14 +0000 http://www.thomasmaurer.ch/?p=763#comment-668515 You had stated it incorrectly as ‘The normal if -eq “$null” doesn’t work:’; if you had used a null string to compare it would have worked:

[String] ${stVar} = ${Null}
PS D:\Users\srhoads\Documents\Dev\PowerShell> if ( “${Null}” -eq ${stVar} ) {write-host “True”} else {write-host “False”}
Returns: True

You can’t compare a string containing a Null to a Null (you have to use a null string “${Null}”).

]]>