Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace NULL value in powershell output

Tags:

powershell

csv

I'm really struggling with what seems to be a simple thing. Any help is appreciated...

tldr; i'm trying to find and replace blank or NULL values from powershell output to "No Data"

I am using the following powershell script to obtain installed application information

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*

Below is sample output of the above script (filtered to my liking). I am taking this data, exporting it to CSV, in order to import into another application for analysis at a later time.

Host             : Computer1
DisplayName      : AutoDWG DWG DXF Converter 2015
Version          : 
Publisher        : 
InstallDate      : 
arrival_datetime : 2015-11-03T09:42:18

Host             : Computer2
DisplayName      : Beyond Compare Version 3.1.11
Version          : 
Publisher        : Scooter Software
InstallDate      : 20150218
arrival_datetime : 2015-11-03T09:42:18

the CSV export puts the data in the correct format, but where items have no data for version, publisher, etc..., it represents it as ",," (see below)

"Computer1","AutoDWG DWG DXF Converter 2015",,,,"2015-11-03T09:54:21"
"Computer2","Beyond Compare Version 3.1.11",,"Scooter Software","20150218","2015-11-03T09:54:21"

When importing the CSV, the blank values are re-interpreted as NULL, which throws the program for a loop, as it is expecting a string. I am attempting to change those to the string "No Data" but am having lots of trouble...

What would be the best way to handle this?

like image 451
bonneyt Avatar asked Jan 08 '23 06:01

bonneyt


2 Answers

Using Select-Object would be your best bet. Just pipe the data to Select-Object, but customize each desired property as follows:

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | 
  Select-Object Host, DisplayName, @{
    Label = "Version"
    Expression = { if ($_.Version) { $_.Version } else { "No Data" } }
  }, Other Properties
like image 159
Benjamin Hubbard Avatar answered Jan 09 '23 19:01

Benjamin Hubbard


You could inspect the property values as they are piped from the Import-Csv cmdlet, and change them. Example:

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | 
ForEach-Object { 
foreach ($p in $_.PSObject.Properties) 
{ 
 if ($p.Value -eq [string]::Empty)
 {
  $p.Value = 'No Data'
 }
} 
 Write-Output $_
}
like image 28
dugas Avatar answered Jan 09 '23 21:01

dugas