4

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?

1
  • Are you asking for empty strings to be changed to "No Data" during export or import? Commented Nov 3, 2015 at 16:21

5 Answers 5

4

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
Sign up to request clarification or add additional context in comments.

3 Comments

The custom object was there to demonstrate the creation of the object in the example, not to show it was necessary. After posting I deleted my answer because after some thought, I think there is a better way in which you don't have to make explicit the conversion for each property.
Well, i'm not importing anything... I'm using the get-itemproperty cmdlet to parse a registry key. that data gets exported to CSV, but that's not really the point. Looking at your select-object example, i think i can make it work.
Modified the example to reflect that.
4

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 $_
}

1 Comment

Thanks for the idea of using $_.PSObject.Properties! Was able to a modified version of that (since I ONLY wanted nulls, and because it doesn't handle an int that is 0 properly). instead of the [string]::empty, used $null. Means you don't need to write something to handle every field that comes through. Definitely The Powershell Way to do this.
1

Building off of this answer, you could generate the calculated properties like this:

$SelectProperties = "Host","DisplayName"
$NoDataFields = "Version","Publisher","InstallDate"

$SelectProperties += $NoDataFields|ForEach-Object {
    @{
      Label = $_
      Expression = [scriptblock]::Create("if(`$_.""$_""){ `$_.""$_"" } else { ""No Data"" }")
    }
}

$Data = Import-Csv -Path "C:\SomePath.csv" | 
  Select-Object $SelectProperties

Comments

0

Do your columns have headers? This was how I did it, when I exported a CSV of groups and their owners. For any group that didn't have an owner in the "ManagedBy" column, it fills the field with "No Owner" instead of a blank space:

$CSVData = Import-Csv $TempCSV -Header "samAccountName", "ManagedBy"  
$CSVData | %{
if($_.ManagedBy -eq "") {$_.ManagedBy="No Owner"}
} 
$CSVData | Export-Csv $Filename -NoTypeInformation

You could just change the "ManagedBy" to your header name and the "$_.ManagedBy" to what you need, then obviously "No Owner" would be "No Data".

Comments

0

Another option that might work:

$TargetFile = "C:\temp\replaceNull.csv"
$File = Get-Content $TargetFile
$Output = @()
foreach ($Line in $File) {
    $Output += $Line -replace ",,",",No Data,"
}
$Output | Set-Content $TargetFile

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.