# Checking for NULL in PS

**URL:** https://forums.powershell.org/t/checking-for-null-in-ps/10925
**Category:** PowerShell Help
**Created:** [July 4, 2018, 2:11am UTC](https://forums.powershell.org/t/checking-for-null-in-ps/10925 "2018-07-04T02:11:00Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![rtan2010](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rtan2010/32/1173_2.png) [@rtan2010](https://forums.powershell.org/u/rtan2010)
#### Post date: [July 4, 2018, 2:11am UTC](https://forums.powershell.org/t/checking-for-null-in-ps/10925/1 "2018-07-04T02:11:00Z")

</div>

Hello Scripting Experts,

I have extracted a simple file via the Import-Excel command as follows:  
\> Import-Excel $infile -Noheader -Dataonly -Startrow 2 | Select P2,P3,P4,P5 |  
Foreach-Object { If ($\_.P2 = NULL ???) …

My objective is to check the property P2 for a NULL (i.e., empty) value (not quite the same as blanks).  
Will the following lines work: (the first line assigns to $Null the empty string using single-quote)  
$Null = ‘’  
ForEach-Object { If (($\_.P2 -EQ $Null) -EQ $True) { true branch } Else { … }?

Would be grateful for any tips, advice or suggestions.

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [July 4, 2018, 2:59am UTC](https://forums.powershell.org/t/checking-for-null-in-ps/10925/2 "2018-07-04T02:59:04Z")

</div>

The variable **$null** already exists on Powershell … always. You don’t need to create or assign. If you like to check for $null you can simply do

```
if ($_.P2 -eq $null) {…}
```

---

<div class="post-metadata">

### Author: ![firmbyte](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@firmbyte](https://forums.powershell.org/u/firmbyte)
#### Post date: [July 4, 2018, 6:09am UTC](https://forums.powershell.org/t/checking-for-null-in-ps/10925/3 "2018-07-04T06:09:31Z")

</div>

I use this

```
if([string]::IsNullOrWhiteSpace($variabletocheck))
```

---

<div class="post-metadata">

### Author: ![rtan2010](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rtan2010/32/1173_2.png) [@rtan2010](https://forums.powershell.org/u/rtan2010)
#### Post date: [July 5, 2018, 9:56am UTC](https://forums.powershell.org/t/checking-for-null-in-ps/10925/4 "2018-07-05T09:56:35Z")

</div>

Many thanks, much appreciated.

---

<div class="post-metadata">

### Author: ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)
#### Post date: [July 5, 2018, 10:26am UTC](https://forums.powershell.org/t/checking-for-null-in-ps/10925/5 "2018-07-05T10:26:07Z")

</div>

One other possibility that is a little more a case-by-case basis is that you can use the fact that when cast to bool, $null becomes $false, and most other things that contain data become $true. So you can do:

```
if (-not $_.P2) { 
# code here 
}
```

If $\_.P2 is either $null, zero, a zero-length array, or contains the value $false, the if statement would execute. Just about anything else is $true and would skip the code in the script block.

---

<div class="post-metadata">

### Author: ![firmbyte](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@firmbyte](https://forums.powershell.org/u/firmbyte)
#### Post date: [July 5, 2018, 11:14am UTC](https://forums.powershell.org/t/checking-for-null-in-ps/10925/6 "2018-07-05T11:14:53Z")

</div>

“_If $\_.P2 is either $null, zero, a zero-length array, or contains the value $false, the if statement would execute_”  
But not if it’s Whitespace.

```
$P2 = " "

if ( -Not $P2) { 
Write-Host "White space" 
} else { Write-Host "something" }

if([string]::IsNullOrWhiteSpace($P2)) { 
Write-Host "White space"
} else { Write-Host "something" }
```

---

<div class="post-metadata">

### Author: ![rtan2010](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rtan2010/32/1173_2.png) [@rtan2010](https://forums.powershell.org/u/rtan2010)
#### Post date: [July 5, 2018, 12:07pm UTC](https://forums.powershell.org/t/checking-for-null-in-ps/10925/7 "2018-07-05T12:07:40Z")

</div>

Thank you all Messrs. Barnetson, Sallow and Soyk. Your replies shed a lot of light and provide many interesting highlights. I have performed your examples hands-on, and I am extremely grateful for your posts. In the course of my “immersion” with NULL, I came across a very informative and interesting site/post, with a short article on the same subject:

> **[Checking for null in PowerShell](https://www.codykonior.com/2013/10/17/checking-for-null-in-powershell/)**
>
> Updated 29 Sep 2016: Fixed a bug in IsNull where Bruno Martins pointed out it returned True for 0. This said… this post is super old… and should be ignored.W...

I thought it interesting to extract that portion of the article to share this author’s “all in one” NULL function:  
#-----------------------------------------------------  
function IsNull($objectToCheck) {  
if ($objectToCheck -eq $null) { return $true }

```
if ($objectToCheck -is [String] -and $objectToCheck -eq [String]::Empty) { return $true }

if ($objectToCheck -is [DBNull] -or $objectToCheck -is [System.Management.Automation.Language.NullString]) { return $true }
return $false

```

}  
#----------------------------------------------------  
Sincerest thanks to you all for sharing your expertise.

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/checking-for-null-in-ps/10925/8 "2024-05-16T20:34:44Z")

</div>


