Reputation: 27
I am using powershell to import a csv file that contains information about books. Like: title, idnum, author,etc... I am trying to compare each property to see if it is equal to "" or null. So what i have is:
Import-Csv C:\location.csv | Foreach-Object{
foreach($property in $_.PSObject.Properties)
{
if($Property -eq "")
{
Write-Host yes
}
}
}
I know that simply saying -eq "" doesnt always work for null values, but the problem is that even if i put -eq "Gooden", and Gooden does exist several times in the list, the if statement doesnt work right. No property is ever equal to "Gooden"
Upvotes: 1
Views: 73
Reputation: 36332
Personally I would deal with it being a little slower and use the [String]
method IsNullOrWhitespace()
if you are looking for properties that have blank or null values. I suppose a RegEx match to "^\w*?$"
and an -or $_ -eq $null
would do the same, but this seems cleaner to me and why re-invent the wheel?
Now, do you want to check all properties, or just specific ones? If you want to see all entries that have any field empty you could do something like:
$AllBooks = import-csv c:\path\to\file.csv
$Props = $AllBooks[0] | Get-Member -MemberType Properties | Select -Expand Name
ForEach($Book in $AllBooks){
If(($Props | Where{[String]::IsNullOrWhiteSpace($Book.$_)}).Count -gt 0){$Book}
}
If you are not concerned with entries that are whitespace you can use IsNullOrEmpty()
which is faster, or you can do [String]:IsNullOrEmpty($Book.$_.Trim())
to remove any whitespace.
That code would import the CSV, collect a list of possible properties from the first entry (with an imported CSV all records should have the same properties so this should be safe). Then for each record it would cycle the list of properties, and check if any of them are empty for that record, and output the record if it is. If you wanted to collect those entries you could just collect the entire ForEach
loop in a variable. Something like:
$BadEntries = ForEach($Book in $AllBooks){
Then you would have $BadEntries to work with.
If you are only concerned with one field, say 'Title', this becomes ridiculously simple...
$AllBooks | Where{[string]::IsNullOrEmpty($_.Title.Trim())}
Upvotes: 1
Reputation: 13567
If you need to determine if the property is null, PowerShell has a built-in vriable for comparisons just like this one.
Additionally, you need to use quotes in order to use Write-Host, so here is an example of how to see if a property is null, and if so, write the name of the property to screen.
If ($property.Value -eq $null){
Write-host "$($property.Name) - This property is null"}
That being said, this is a strange request you're making. People often ask us how to do bizarre things here on Stack, when they really need to do something common and aren't asking how to do it.
With that being said, what is your end-goal here, beyond this one technical question?
If you can elaborate, I might be able to help you with a better approach.
Upvotes: 1
Reputation: 72680
You should first work with properties name like this :
Import-Csv C:\location.csv | Foreach-Object {
$a=$_;
$_.Psobject.properties | % {
if ($a.$($_.name)-eq 0){
Write-Host "$($_.name) is 0"}
}
}
Upvotes: 1