# Filtering out null values

**URL:** <https://forums.powershell.org/t/filtering-out-null-values/23439>\
**Category:** PowerShell Help\
**Created:** [January 19, 2024, 5:17am UTC](https://forums.powershell.org/t/filtering-out-null-values/23439 "2024-01-19T05:17:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dalebert](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@dalebert](https://forums.powershell.org/u/dalebert)\
**Post date:** [January 19, 2024, 5:17am UTC](https://forums.powershell.org/t/filtering-out-null-values/23439/1 "2024-01-19T05:17:46Z")

</div>

Hi, All.

# Would anyone happen to know how to filter out blank values after a query? For example if I run:

Get-Printer -ComputerName PrintServer1 | where {$\_.Name -match “GYY54-East”} | select \*

I get the following values:

# RenderingMode : PrinterStatus : Normal Type : Local DeviceType : Print Caption : Description : ElementName : InstanceID : CommunicationStatus : DetailedStatus : HealthState : InstallDate : Name : GYY54-East OperatingStatus : OperationalStatus : PrimaryStatus : Status : StatusDescriptions : BranchOfficeOfflineLogSizeMB : Comment : Test Printer - 565 N. Hetfield Way Chicago, IL ComputerName : printserver1 Datatype : RAW DefaultJobPriority : 0 DisableBranchOfficeLogging : DriverName : Universal v2 JobCount : 0 KeepPrintedJobs : False Location : Chicago/U.S.A PermissionSDDL : PortName : IP\_192.168.0.121 PrintProcessor : RIUD3P4C Priority : 1 Published : True SeparatorPageFile : Shared : True ShareName : GYY54-East StartTime : 60 UntilTime : 60 WorkflowPolicy : PSComputerName : CimClass : ROOT/StandardCimv2:MSFT\_Printer CimInstanceProperties : {Caption, Description, ElementName, InstanceID…} CimSystemProperties : Microsoft.Management.Infrastructure.CimSystemProperties

Is there a way to filter out the properties that have no values to them? I tried a simple ForEach to go through each property and not include it in the final output if the value is blank. But that didn’t go very well. I’ve never had to run through something like this, so I’m not sure where to begin.

Any help would be fabulous.

Thanks in advance.

---

<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:** [January 19, 2024, 2:16pm UTC](https://forums.powershell.org/t/filtering-out-null-values/23439/2 "2024-01-19T14:16:01Z")

</div>

Hi Dalebart,

just a reminder to format your code as code. It makes things hard to read. I sent you a link in another thread on how to do that.

What problem are you trying to solve by doing this? I don’t suggest trying to do that. Instead of worrying about filtering out null ones, why don’t you just select the properties you want instead.

If you really wanted to do that, you’d likely need to look through the properties in some way, figure out which are null, then build a filter based on that (or create a new object). Usually I use `Get-Member` to view properties, but since it has more than just properties, that could get a little tricky. Perhaps another method would be to use the PSObject property, EG `$thing.psobject.properties.name` to get a list of properties an object has, and use that as a loop mechanism. Note that I feel like I read it doesn’t work with certain property types, but it should probably work for this application:

```powershell
$Printer = Get-Printer | Where-Object {$_.name -like 'HP69*'}
$Properties = $printer.psobject.Properties.Name

$FilteredProperties = $Properties | ForEach-Object {
    if (-not [string]::IsNullOrEmpty($Printer.$_)) {
        $_
    }
}

$SelectObjectFilter = @{
    Property = $FilteredProperties
}

$Printer | Select-Object @SelectObjectFilter

```

Note I’m using logic for null or empty, because at least with my local testing, it seems like some things that appeared to be null, weren’t actually, and had white space (like the comment property)

---

<div class="post-metadata">

**Author:** ![dalebert](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@dalebert](https://forums.powershell.org/u/dalebert)\
**Post date:** [January 19, 2024, 4:50pm UTC](https://forums.powershell.org/t/filtering-out-null-values/23439/3 "2024-01-19T16:50:04Z")

</div>

I noticed after I posted the question that I didn’t format the output. My only excuse on that is I was mentally fried by the time I put this message up and wasn’t thinking about it. It’s annoying, I know. I’ll do better.

The reason for wanting to filter the null values is because I don’t know what will, or won’t, be (or seem to be) empty in the output. So, I thought filtering out the nulls at time of query would be good to clean things up and give me just the data that is present. In my feeble attempt I tried to loop through, using the same kind of condition that you had;

> if (-not [string]::IsNullOrEmpty($Printer.$\_))

But my condition didn’t use the IsNullOrEmpty and just errored.

This is just one component to a much larger PS form application I’m putting together for my team. Many of the guys I work with don’t have much of an idea on how to use PS (not like I do either, but I’m trying to learn). So, I’m attempting to make my team a little more efficient in the day-to-day tasks, and hoping this will help us out.

Anyway, I appreciate the help and patience with my questions. Thank you!

---

<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:** [January 19, 2024, 5:45pm UTC](https://forums.powershell.org/t/filtering-out-null-values/23439/4 "2024-01-19T17:45:57Z")

</div>

No worries. did the code I share work for you?

---

<div class="post-metadata">

**Author:** ![dalebert](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@dalebert](https://forums.powershell.org/u/dalebert)\
**Post date:** [January 19, 2024, 7:33pm UTC](https://forums.powershell.org/t/filtering-out-null-values/23439/5 "2024-01-19T19:33:49Z")

</div>

It worked perfectly. Thanks again. I had to understand what a PSObject really meant. I’ve used them, but didn’t had a clear understanding of what they were. It makes sense now how your code is stripping out the empty values.

I’ll need to play around with it a bit to get my head wrapped around it, but It’s coming together now. Again, thanks for the help!

---

<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:** [January 20, 2024, 6:17am UTC](https://forums.powershell.org/t/filtering-out-null-values/23439/6 "2024-01-20T06:17:57Z")

</div>


