How can I filter out empty printer columns from my CSV?

0
13
Asked By CoolCat42 On

I'm trying to work with a CSV file that lists printers for different rooms. My main goal is to only display the printers that actually have data when I search for a specific room. For instance, when I run my command for LAB01, it currently shows both Printer01 and Printer02 even if Printer02 is empty. I want to refine my query to exclude any empty printer columns, so I only get the relevant printers listed. Here's the command I'm currently using:

Import-Csv -Path "$PSScriptRootRooms.csv" | Where-Object {$_.ROOM -like "*$DeviceName*"} | Select-Object Printer*

Here's what my CSV looks like:
|ROOM|PRINTER01|PRINTER02|
|:-|:-|:-|
|LAB01|HP 533||
|LAB02|HP 505|HP 1606|

3 Answers

Answered By TechWhiz88 On

If you want to exclude any row with empty printer values, you could modify your command like this:

`Import-Csv -Path "$PSScriptRootRooms.csv" | Where-Object {$_.ROOM -like "*$DeviceName*" -and ($_.PRINTER01 -ne '' -or $_.PRINTER02 -ne '')}` This checks if at least one printer has value before including the row.

Answered By QuickFixPro On

Here's a quick two-liner that might help:

```PowerShell
$Room = Import-Csv -Path "$PSScriptRootRooms.csv" | Where-Object {$_.ROOM -like "*$DeviceName*"}
@($Room.'PRINTER01', $Room.'PRINTER02').Where{$_}
``` This grabs the relevant room and then filters out any empty printer values from the output.

Answered By DataDude99 On

You might have to use a loop to filter out specific columns that have data. The way you’re set up, the command will show all columns regardless of the content, so creating a custom output might be necessary. Here's a simple loop you could try:

```PowerShell
$deviceName = "LAB01"
$rooms = Import-Csv C:usersPublictest.csv
foreach ($room in $rooms) {
if ($room.ROOM -like "$deviceName*") {
$room | Where-Object { $_.PRINTER01 -or $_.PRINTER02 }
}
}
``` This will iterate through and return only the rows with data in either printer column.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.