# How to check if a property is null/empty in a CSV

**URL:** https://forums.powershell.org/t/how-to-check-if-a-property-is-null-empty-in-a-csv/14230
**Category:** PowerShell Help
**Created:** [April 23, 2020, 3:35am UTC](https://forums.powershell.org/t/how-to-check-if-a-property-is-null-empty-in-a-csv/14230 "2020-04-23T03:35:04Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![tim-97](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tim-97/32/1769_2.png) [@tim-97](https://forums.powershell.org/u/tim-97)
#### Post date: [April 23, 2020, 3:35am UTC](https://forums.powershell.org/t/how-to-check-if-a-property-is-null-empty-in-a-csv/14230/1 "2020-04-23T03:35:04Z")

</div>

Hi,

&nbsp;

I have a CSV with several properties like PersonalArea;Employeenumber;…

I want to count the lines in this CSV but only these where the property “Employeenumber” is not null and the PersonalArea is equal to “myLocation”.

Currently, I got the following code:

&nbsp;

```
$filepath = "C:path\to\csv"
$CSV = Import-Csv -Path $filepath -Delimiter ";" -Encoding UTF8 | Measure-Object
$a = $CSV | Where-Object {$_.PersonalArea -eq "myLocation -and $_.Employeenumber -ne $null}
#count lines where the expression is true
$a.count
#count all lines except op the first one
$CSV.count
```

Here is an CSV example:

PersonalArea;Employeenumber

myLocation;123456

myLocation;234567

otherLocation;345678

otherLocation;

&nbsp;

Currently, I get the following output:

$a.count –\> 0

$CSV.count –\> 4

&nbsp;

Can someone tell me why I getting a wrong output on $a.count?

I appreciate your help.

---

<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: [April 23, 2020, 3:38am UTC](https://forums.powershell.org/t/how-to-check-if-a-property-is-null-empty-in-a-csv/14230/2 "2020-04-23T03:38:45Z")

</div>

If I got it right you should check for an empty string instead of $null because Import-Csv only returns strings not objects.

---

<div class="post-metadata">

### Author: ![tim-97](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tim-97/32/1769_2.png) [@tim-97](https://forums.powershell.org/u/tim-97)
#### Post date: [April 23, 2020, 3:41am UTC](https://forums.powershell.org/t/how-to-check-if-a-property-is-null-empty-in-a-csv/14230/3 "2020-04-23T03:41:31Z")

</div>

I already tested it out with an empty string…same result

```
$a = $CSV | Where-Object {$_.PersonalArea -eq "myLocation -and $_.Employeenumber -ne ""}
```

OR

```
$a = $CSV | Where-Object {$_.PersonalArea -eq "myLocation -and $_.Employeenumber -ne ''}
```

---

<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: [April 23, 2020, 3:49am UTC](https://forums.powershell.org/t/how-to-check-if-a-property-is-null-empty-in-a-csv/14230/4 "2020-04-23T03:49:45Z")

</div>

And why not just

```
$CSV | Where-Object {$_.PersonalArea -eq "myLocation -and $_.Employeenumber}
```

---

<div class="post-metadata">

### Author: ![tim-97](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tim-97/32/1769_2.png) [@tim-97](https://forums.powershell.org/u/tim-97)
#### Post date: [April 23, 2020, 3:56am UTC](https://forums.powershell.org/t/how-to-check-if-a-property-is-null-empty-in-a-csv/14230/5 "2020-04-23T03:56:44Z")

</div>

I didn’t know this works. But it doesn’t work either.

---

<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: [April 23, 2020, 4:14am UTC](https://forums.powershell.org/t/how-to-check-if-a-property-is-null-empty-in-a-csv/14230/6 "2020-04-23T04:14:16Z")

</div>

It does work for me with my sample data. 😉 Could you please post **a few** sanitized but still representative rows of your CSV file? Please format it as code.

---

<div class="post-metadata">

### Author: ![tim-97](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tim-97/32/1769_2.png) [@tim-97](https://forums.powershell.org/u/tim-97)
#### Post date: [April 23, 2020, 4:31am UTC](https://forums.powershell.org/t/how-to-check-if-a-property-is-null-empty-in-a-csv/14230/7 "2020-04-23T04:31:26Z")

</div>

I’m using the same CSV data for testing as I mentioned above. Can you post your code?

```
PersonalArea;Employeenumber
myLocation;123456
myLocation;234567
otherLocation;345678
otherLocation;
```

---

<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: [April 23, 2020, 4:45am UTC](https://forums.powershell.org/t/how-to-check-if-a-property-is-null-empty-in-a-csv/14230/8 "2020-04-23T04:45:21Z")

</div>

```
$CSVData = @'
PersonalArea;Employeenumber
myLocation;123456
myLocation;234567
otherLocation;345678
otherLocation;
'@ | ConvertFrom-Csv -Delimiter ';'

($CSVData | Where-Object {$_.Employeenumber}).count

$CSVData.Count
```

That’s the actual code I’m using to check if it works.  
The output is this:

```
PS C:\> $CSVData = @'
PersonalArea;Employeenumber
myLocation;123456
myLocation;234567
otherLocation;345678
otherLocation;
'@ | ConvertFrom-Csv -Delimiter ';'

($CSVData | Where-Object {$_.Employeenumber}).count

$CSVData.Count
3
4
PS C:\>
```

---

<div class="post-metadata">

### Author: ![tim-97](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tim-97/32/1769_2.png) [@tim-97](https://forums.powershell.org/u/tim-97)
#### Post date: [April 23, 2020, 5:09am UTC](https://forums.powershell.org/t/how-to-check-if-a-property-is-null-empty-in-a-csv/14230/9 "2020-04-23T05:09:22Z")

</div>

Now it works. First I have added the brackets + .count

```
($CSVData | Where-Object {$_.Employeenumber}).count
```

Then I removed these changes and tested it again with my code…

```
$filepath = "C:\path\to\csv"
$CSV = Import-Csv -Path $filepath -Delimiter ";" -Encoding UTF8
$a =$CSV | Where-Object {$_.PersonalArea -eq "myLocation" -and $_.Employeenumber}
$a.count
#count lines where the expression is true
#$a.count
#count all lines except op the first one
$CSV.count
```

The output is correct in both test cases.

Thank you. Could you explain why you are using $_.Employeenumber instead of $_.Employeenumber -ne ’ ’ ? Is it just shorter or is there another reason?

I think the problem was the piping of Measure-Object

```
$CSV = Import-Csv -Path $filepath -Delimiter ";" -Encoding UTF8 | Measure-Object
```

---

<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: [April 23, 2020, 6:46am UTC](https://forums.powershell.org/t/how-to-check-if-a-property-is-null-empty-in-a-csv/14230/10 "2020-04-23T06:46:51Z")

</div>

> [@](#):
>
> I think the problem was the piping of Measure-Object

Ooops … I missed that completely until you just mentioned it now. Sorry. 😉

> [@](#):
>
> Could you explain why you are using $_.Employeenumber instead of $_.Employeenumber -ne ‘ ‘ ? Is it just shorter or is there another reason?

Mostly I’m really bad with explaining of what’s going on under the hood. Personally I like to write the code as short as possible but still as descriptive and detailed as possible without using aliasses or abbreviations.

BTW: Sometimes it’s easier to ask a quesiotn in your native language. There are German Powershell-related forums as well like [MCSEBoard.de](https://www.mcseboard.de/forum/71-windows-forum-%E2%80%94-scripting/) or the [German Microsoft Technet Powershell forum](https://social.technet.microsoft.com/Forums/de-de/home?forum=powershell_de&filter=alltypes&sort=lastpostdesc).

Tschüß 😉

---

<div class="post-metadata">

### Author: ![tim-97](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tim-97/32/1769_2.png) [@tim-97](https://forums.powershell.org/u/tim-97)
#### Post date: [April 23, 2020, 6:51am UTC](https://forums.powershell.org/t/how-to-check-if-a-property-is-null-empty-in-a-csv/14230/11 "2020-04-23T06:51:47Z")

</div>

> [@](#):
>
> BTW: Sometimes it’s easier to ask a quesiotn in your native language. There are German Powershell-related forums as well like [MCSEBoard.de](http://MCSEBoard.de) or the German Microsoft Technet Powershell forum.

Danke für den Tipp 🙂 Vielen Dank für die Hilfe.

Tschüss.

---

<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:30pm UTC](https://forums.powershell.org/t/how-to-check-if-a-property-is-null-empty-in-a-csv/14230/12 "2024-05-16T20:30:11Z")

</div>


