0

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 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

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.