How to clear extra lines in output while running powershell sript and Output CSV file

D Vijayakumar VLR 126 Reputation points
2021-04-02T03:39:06.757+00:00

Hi All,

I have script for service status check service is running or stopped but while run the script output results shows in script itself and same will come in CSV output reportfile . Please help on this issue it will be very helpfull.

Service Statdu Check Scripts

$Servers = Get-Content -Path "C:\Users\CPCWM\Desktop\Services Status Pre patch checking\Serverlist.txt"
$Services = Get-Content -Path "C:\Users\CPCWM\Desktop\Services Status Pre patch checking\Services.txt"
$Date = Get-Date -Format "dd-mm-yy hh:mm"
$Servreportpre = $null
$Servreportpre = @()
$x=0
foreach($Server in $Servers)
{ $x=$x+1
Write-Host "Service Status StartType for Server $x :$Server" -ForegroundColor Yellow
foreach($Service in $Services)
{
$Status = Get-WmiObject Win32_Service -ComputerName $Servers | ?{$_.Name -eq $Service} | Select-Object -ExpandProperty "State"
$StartType = Get-WmiObject Win32_Service -ComputerName $Servers | ?{$.Name -eq $Service} | Select-Object -ExpandProperty "StartMode"
if($Status -like "Running")
{
Write-Host "$Service,$Status,$StartType" -ForegroundColor Green
$Servreportpre += $Server | Select @{n='ServerName';e={$
}}, @{n='ServiceName';e={"$Service"}}, @{n='Status';e={"$Status"}}, @{n='StartType';e={"$StartType"}}
}
elseif($Status -like "Stopped")
{
Write-Host "$Service,$Status,$StartType" -ForegroundColor Red
$Servreportpre += $Server | Select @{n='ServerName';e={$_}}, @{n='ServiceName';e={"$Service"}}, @{n='Status';e={"$Status"}}, @{n='StartType';e={"$StartType"}}
}
}
}
$Pdate = Get-Date -Format "dd-mm-yy hh:mm"
$Servreportpre | Export-Csv -Path "C:\Users\CPCWM\Desktop\Services Status Pre patch checking\Service Status Pre Check Report.csv" -NoTypeInformation

Output in CSV File

83912-![83892-output-in-csv-file.jpg83921-powershell-script-output-with-error.jpgimage.png]1

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,176 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,971 Reputation points Microsoft Vendor
    2021-04-02T04:35:04.087+00:00

    Hi,

    If no output is wanted you can simply remove the ”Write-Host“ lines.

    $Servers = Get-Content -Path "C:\Users\CPCWM\Desktop\Services Status Pre patch checking\Serverlist.txt"  
    $Services = Get-Content -Path "C:\Users\CPCWM\Desktop\Services Status Pre patch checking\Services.txt"  
    $Date = Get-Date -Format "dd-mm-yy hh:mm"  
    $Servreportpre = $null  
    $Servreportpre = @()  
    $x=0   
    foreach($Server in $Servers)  
    {  
        $x=$x+1  
        Write-Host "Service Status StartType for Server $x :$Server" -ForegroundColor Yellow  
        foreach($Service in $Services)  
        {  
            $Status = Get-WmiObject Win32_Service -ComputerName $Servers | ?{$_.Name -eq $Service} | Select-Object -ExpandProperty "State"  
            $StartType = Get-WmiObject Win32_Service -ComputerName $Servers | ?{$_.Name -eq $Service} | Select-Object -ExpandProperty "StartMode"  
            if($Status -like "Running")  
            {  
                $Servreportpre += $Server | Select @{n='ServerName';e={$_}}, @{n='ServiceName';e={"$Service"}}, @{n='Status';e={"$Status"}}, @{n='StartType';e={"$StartType"}}  
            }  
            elseif($Status -like "Stopped")  
            {  
                $Servreportpre += $Server | Select @{n='ServerName';e={$_}}, @{n='ServiceName';e={"$Service"}}, @{n='Status';e={"$Status"}}, @{n='StartType';e={"$StartType"}}  
            }  
        }  
     }  
    $Pdate = Get-Date -Format "dd-mm-yy hh:mm"  
    $Servreportpre | Export-Csv -Path "C:\Users\CPCWM\Desktop\Services Status Pre patch checking\Service Status Pre Check Report.csv" -NoTypeInformation  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments