0

I've got an script and I want to remove the white spaces that powershell puts by default in the output result. Is there any way of doing it?

=======Computer1=======
Microsoft Windows 8.1 Pro


Name         : Computer1
Model        : Vostro 200
Manufacturer : Dell Inc.





SerialNumber : 012345

This is what I want:

=======Computer1=======
Microsoft Windows 8.1 Pro
Name         : Computer1
Model        : Vostro 200
Manufacturer : Dell Inc.
SerialNumber : 012345

This is my script:

$Computers=Import-Csv C:\Powershell\test.csv
$ResultsPath="C:\Powershell\test.txt"


foreach ($i in $Computers.Name) {
         "="*7 + $i + "="*7 
         if (Test-Connection $i -quiet) {
         (Get-WmiObject -class Win32_OperatingSystem -ComputerName $i).Caption
         Get-WmiObject -class Win32_Computersystem -ComputerName $i | Select-Object Name, Model, Manufacturer | Format-List
         Get-WmiObject win32_SystemEnclosure -ComputerName $i | Select-Object SerialNumber | Format-List }
         else { "nothing" }
         }
Kara
  • 6,115
  • 16
  • 50
  • 57
miticoluis
  • 511
  • 2
  • 15
  • 32

2 Answers2

0

Convert output to string and trim it:

     "="*7 + $i + "="*7 
     if (Test-Connection $i -quiet) {
         (Get-WmiObject -class Win32_OperatingSystem -ComputerName $i).Caption
         (Get-WmiObject -class Win32_Computersystem -ComputerName $i | Select-Object Name, Model, Manufacturer | Format-List | Out-String).Trim()
         (Get-WmiObject win32_SystemEnclosure -ComputerName $i | Select-Object SerialNumber | Format-List | Out-String).Trim() }
     else { "nothing" }
Deadly-Bagel
  • 1,612
  • 1
  • 9
  • 21
0

While Trim will do what you need, this is not a PowerShell way. Here is revised script, that works with objects internally and writes output the way you want.

$Computers = Import-Csv 'C:\Powershell\test.csv'
$ResultsPath = 'C:\Powershell\test.txt'

foreach ($i in $Computers.Name) {
    $Header = '='*7 + $i + '='*7
    Write-Output $Header

    if (Test-Connection $i -quiet)
    {
        $Os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $i | Select-Object -ExpandProperty Caption
        $Info = Get-WmiObject -Class Win32_Computersystem -ComputerName $i  | Select-Object Name, Model, Manufacturer
        $Sn = Get-WmiObject -Class Win32_SystemEnclosure -ComputerName $i | Select-Object -ExpandProperty SerialNumber

        $PC = New-Object -TypeName psobject -Property @{
            OperatingSystem = $Os
            Name = $Info.Name
            Model = $Info.Model
            Manufacturer = $Info.Manufacturer
            SerialNumber = $Sn
        }   | Select-Object OperatingSystem, Name, Model, Manufacturer, SerialNumber

        Write-Output ($PC | Format-List | Out-String).Trim()
    }
    else
    {
        Write-Output 'nothing'
    }
}
beatcracker
  • 6,714
  • 1
  • 18
  • 41
  • Thank you very much beatcracker but now the output order of the values of PC object are changed. I want them to be shown strictly in the order of the PC object declaration (1st Operating System, 2nd Name, 3rd Model, 4th Manufacturer, 5thSerialNumber) – miticoluis Feb 17 '15 at 16:42
  • Aah, that would be because hashtables do not preserve order, and I've used one to create an object. Hold on, I'll fix this and update my answer. – beatcracker Feb 17 '15 at 16:51