Hi Everyone,
I am trying to remove blank lines from output (PSCustomObject) $Obj before exporting it to CSV, but nothing works for me
$Services = Import-Csv “C:\Services.csv”
ForEach ($Service in $Services){
Try{
$ServiceStatus = $Service.Status
$ServiceName = $Service.Name
$Hash = [ordered]@{ServiceStatus = $ServiceStatus
ServiceName = $ServiceName}
}
Catch{}
Finally {
If($hash.ServiceStatus)
{}
Else
{$hash.remove(‘servicename’)}
$Obj = New-Object -TypeName PSOBject -Property $hash
$Obj
}
}
Status,Name
Stopped,AppIDSvc
,Appinfo
Running,Bits
Above is then CSV I am Importing, My end goal is where there is no value in status I want to remove whole line from CSV Including values for Status and Name, which works for me in above script, now blank line is remaining after removing those values, I need to remove that blank line as final result
ServiceStatus ServiceName
Running AppIDSvc
Stopped Bit
# Omit rows with no value for status header then export
Import-Csv -Path “C:\Services.csv” | Where-Object {$_.Status} |
Export-Csv -Path “C:\ServicesWithStatus.csv” -NoTypeInformation
Worked for me. Thank You so much.