Welcome › Forums › General PowerShell Q&A › Remove blank lines from PSCustomObject
- This topic has 3 replies, 2 voices, and was last updated 1 month, 2 weeks ago by
Participant.
-
AuthorPosts
-
-
December 5, 2020 at 12:27 pm #276810
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
}}
-
December 5, 2020 at 12:41 pm #276813
Status,Name
Stopped,AppIDSvc
,Appinfo
Running,BitsAbove 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 AppIDSvcStopped Bit
-
December 5, 2020 at 10:49 pm #276870PowerShell123# Omit rows with no value for status header then exportImport-Csv -Path “C:\Services.csv” | Where-Object {$_.Status} |Export-Csv -Path “C:\ServicesWithStatus.csv” -NoTypeInformation
-
December 6, 2020 at 11:51 am #276921
Worked for me. Thank You so much.
-
-
AuthorPosts
- You must be logged in to reply to this topic.