I am reading a CSV in a powershell script. The script pulls specific data, and then writes it to a different CSV. Is there a way I can replace any "" values with the word "null"?
1 Answer
I think that should give you something to start with...:
@'
Foo,Bar
"",""
Alfa,Beta
Gamma,""
"",Delta
'@ | ConvertFrom-csv | Foreach-Object {
foreach ($Property in $_.PSObject.Properties) {
if ([string]::IsNullOrEmpty($Property.Value)) {
$_.($Property.Name) = 'NULL'
}
}
$_
} | ConvertTo-Csv -NoTypeInformation
You could try just replace: "" with "NULL", but if you have more complicated data it may give you some unexpected results, working on PSObject.Properties collection does not have this flaw/ risk.
1 Comment
Michael Sorens
Thanks for revealing the PSObject; was not aware of that. Do you happen to have any links to articles discussing PSObject? Neither a quick web search nor the PowerShell spec was terribly fruitful.