TechOnTip Weblog

Run book for Technocrats

Powershell: Transfer Output to Txt, CSV, HTML & Variable

Posted by Brajesh Panda on March 30, 2010

Let me summarize all methods on “How to transfer PowerShell Output to HTML for Reporting?

PowerShell Provides Output in multiple formats like Table, List etc. And we can capture these outputs to Txt, CSV, HTML and Variable using below process

# Transfer Output to Text file

è Get-exchangeserver > c:\test1.txt

è Get-exchangeserver | Out-File c:\test2.txt

You’ll notice that the last Colum could not be fully displayed & it is shortened with …. Signs. We have to increase the width of the display screen

è Get-Process | Out-File c:\scripts\test.txt -width 120

# Transfer Output to CSV file

è Get-exchangeserver | Export-Csv c:\test1.csv -NoTypeInformation -force

-NoTypeInformation removes headers

# Transfer Output to HTML file

è Get-exchangeserver | ConvertTo-Html | Set-Content c:\exchange.html

Note: However this command will capture all outputs into the HTML file; I know you will not like that. We can define filter to transfer specific columns to our HTML File

è Get-exchangeserver | ConvertTo-Html name, ExchangeVersion | Set-Content c:\exchange1.html

In this case the HTML file Title will be look like HTML TABLE. Let’s change the HTML file title using below commands;

è Get-exchangeserver | ConvertTo-Html name, ExchangeVersion –title “Exchange Server List” | Set-Content c:\exchange1.html

è Get-exchangeserver | Select-Object name, ExchangeVersion | ConvertTo-Html | Set-Content c:\exchange1.html

# Transfer Output to Variable

è $Variablename = <PowerShellCMDLet>

è PowerShellCMDLet | tee-object -variable <variablename>

Let’s do some beautification to our HTML page by adding our company logo, name etc. It includes create a HTML Style Sheet & Transfer your output to the Style Sheet.

~~~~~~~~Note: Styles to be placed in the header of the HTML file

$a = “<style>”

$a = $a + “BODY{font-family: Verdana, Arial, Helvetica, sans-serif;font-size:10;font-color: #000000}”

$a = $a + “TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}”

$a = $a + “TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color: #E8E8E8}”

$a = $a + “TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black}”

$a = $a + “</style>”

#Body text, pretty much just the report title:

$b = “<img src=’ https://techontip.files.wordpress.com/2009/12/brajesh.jpg‘ height=’80’ width=’100′><H2>List of All Exchange Servers</H2>”

Get-exchangeserver | Select-Object name, ExchangeVersion | ConvertTo-Html -head $a -body $b | Set-Content c:\exchange3.html

~~~~~~~~~~~~~~~

http://www.microsoft.com/technet/scriptcenter/resources/pstips/jan08/pstip0104.mspx

7 Responses to “Powershell: Transfer Output to Txt, CSV, HTML & Variable”

  1. Rob said

    For what it is worth, the Export-Csv function is not as useful as it would be with an append option. You can pass an object or objects in one operation and it works fine, but if you are iterating through a list with foreach(for instance) and calling Export-Csv on each pass, it overwrites or bombs out depending on the clobber switch. Append is available in Out-File, why not in Export-Csv powers-that-be?

  2. Cool blog! I dont think Ive seen all the angles of this subject the way youve pointed them out. Youre a true star, a rock star man. Youve got so much to say and have a lot of knowledge about the subject that I think you should just teach a class about itHaHa!I tried to cover the same thing here about Stanley Tom Mamela,

  3. Hello I didnt had a good read from a long time.Truly glad i found it on yahoo.I was talking with my friends two days ago around this issue and I have to say you make things very clear for me. Thanks and good luck! I tried to cover the same thing here about Stanley Tom Mamela

  4. Also check out https://techontip.wordpress.com/2015/01/08/powershell-html-report-with-multiple-tables/

  5. Andres said

    Thank you for sharing! Very useful information and you did a great job explaining it.

Leave a comment