Formatting Output

Welcome Forums General PowerShell Q&A Formatting Output

This topic contains 2 replies, has 3 voices, and was last updated by

5 years, 7 months ago.

  • Author
  • #9973

    Topics: 1
    Replies: 0
    Points: 0
    Rank: Member

    Hi Experts,

    I'm relatively new to Powershell and I'm having some problems getting things to output in the format that I want. I have created the code below to run through a list of servers, determine the last reboot time, and if a server is pending a reboot. Right now it works alright and outputs to the screen. However it would be nice to also have it output to a CSV and/or out-gridview. Maybe out-gridview isn't the way to do it, but I just learned about it and wanted to try and play with it.

    My problem was when I tried to use out-gridview it would open a new window for each server and object. How do I get all of the servers and information from each time it executes the loop to output to the same window? Is there a way to append to an existing gridview window? Do i need to collate everything before outputting to out-gridview?

    foreach ($server in get-content "./serversTEST.txt")
    #loop through each server and check last reboot and reboot pending status
    Write-Host "$server rebooted last" -BackgroundColor DarkBlue -ForegroundColor Yellow
    $wmi = Get-WmiObject -Class Win32_OperatingSystem -Computer "$server"
    $reboot = [wmiclass]"\\$server\root\ccm\ClientSDK:CCM_ClientUtilities"
    $result = $reboot.DetermineIfRebootPending() | Select RebootPending
    If ($result -like '@{RebootPending=False}')
    {Write-Host $result -ForegroundColor Green}
    {Write-Host $result -ForegroundColor Red}
    Write-Host "" —————————————-

    If somebody could help me over my hurdle and get me on my way to better understanding I would greatly appreciate it.


  • #9974

    Topics: 13
    Replies: 4872
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    It outputs to the screen because Write-Host can't do anything but that. You probably want Write-Output instead. It's kind of a lot of underlying things going on; if you get a sec, grab yourself a copy of "Learn PowerShell 3 in a Month of Lunches." But, suffice to say, Write-Host draws to the screen. Write-Output puts things in the pipeline, where they will end up on the screen if you don't pipe them to a file (Out-File) or something else (Export-CSV). But having output in the pipeline gives you that choice. You just don't get to choose pretty colors.

  • #9976

    Topics: 0
    Replies: 669
    Points: 0
    Rank: Member

    I agree completely with Don regarding “Learn PowerShell 3 in a Month of Lunches.” It is the best book from which to learn PowerShell.

    To solve your problem try something like this

    $servers = "server02", "server03", "exch10"

    $servers | foreach {
    $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $_
    $comp = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $_

    New-Object -TypeName PSObject -Property @{
    Name = $os.PSComputerName
    LastReboot = $os.ConvertToDateTime($os.LastBootUpTime)
    Model = $comp.Model


    I've used Win32_ComputerSystem as my second class because I don't have the ccm namespace you used. The principle is the same. Get the classes and then put the properties you want into an object. Output the object.

    You can then add

    | Out-GridView


    | export-csv data.csv -notypeinformation

    after the last }

    if you read the recommended book you'll learn how to make this code a function you can put into your pipeline to make things even easier for you.

The topic ‘Formatting Output’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort