Remove blank lines from PSCustomObject

Welcome Forums General PowerShell Q&A Remove blank lines from PSCustomObject

Viewing 3 reply threads
  • Author
    Posts
    • #276810
      Participant
      Topics: 10
      Replies: 11
      Points: 145
      Rank: Participant

      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
      }

      }

       

       

    • #276813
      Participant
      Topics: 10
      Replies: 11
      Points: 145
      Rank: Participant

      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

    • #276870
      Participant
      Topics: 5
      Replies: 349
      Points: 632
      Helping Hand
      Rank: Major Contributor
    • #276921
      Participant
      Topics: 10
      Replies: 11
      Points: 145
      Rank: Participant

      Worked for me. Thank You so much.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.