# Remove blank lines from PSCustomObject

**URL:** <https://forums.powershell.org/t/remove-blank-lines-from-pscustomobject/15337>\
**Category:** PowerShell Help\
**Created:** [December 5, 2020, 6:57am UTC](https://forums.powershell.org/t/remove-blank-lines-from-pscustomobject/15337 "2020-12-05T06:57:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aman1313](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@aman1313](https://forums.powershell.org/u/aman1313)\
**Post date:** [December 5, 2020, 6:57am UTC](https://forums.powershell.org/t/remove-blank-lines-from-pscustomobject/15337/1 "2020-12-05T06:57:37Z")

</div>

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  
}

}

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![aman1313](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@aman1313](https://forums.powershell.org/u/aman1313)\
**Post date:** [December 5, 2020, 7:11am UTC](https://forums.powershell.org/t/remove-blank-lines-from-pscustomobject/15337/2 "2020-12-05T07:11:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [December 5, 2020, 5:19pm UTC](https://forums.powershell.org/t/remove-blank-lines-from-pscustomobject/15337/3 "2020-12-05T17:19:31Z")

</div>

```
# 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
```

---

<div class="post-metadata">

**Author:** ![aman1313](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@aman1313](https://forums.powershell.org/u/aman1313)\
**Post date:** [December 6, 2020, 6:21am UTC](https://forums.powershell.org/t/remove-blank-lines-from-pscustomobject/15337/4 "2020-12-06T06:21:00Z")

</div>

Worked for me. Thank You so much.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/remove-blank-lines-from-pscustomobject/15337/5 "2024-05-16T20:28:12Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


