- Create a certificate-signed RDP shortcut via Group Policy - Fri, Aug 9 2019
- Monitor web server uptime with a PowerShell script - Tue, Aug 6 2019
- How to build a PowerShell inventory script for Windows Servers - Fri, Aug 2 2019
Let's say you've developed this excellent script that pulls all kinds of information from various sources that save you and your team tons of time. This script pulls information from Active Directory, some HR database, your VMware/Hyper-V nodes and so on. How it correlates all of that information is really a huge time-saver for you.
You present it to a non-technical manager; he looks at the text output and his eyes glaze over. He can't understand the default PowerShell console output. Regardless of how useful you think the information is, if you can't convey that usefulness to non-technical people, it probably won't work.
PowerShell, as we know, is a command-line tool. It takes input from the command line and sends output to the console in text form. It doesn't have to be this way though. By using a built-in cmdlet and a little HTML/CSS wizardry, you can build reports in HTML that have tables, color and a whole lot more. To do this requires using the ConvertTo-Html cmdlet.
By default, when PowerShell returns a table of information, it will have multiple columns and rows.
This is typically a useful way to present the information. However, it's not necessarily visually appealing. PowerShell provides a built-in cmdlet called ConvertTo-Html. This takes objects as input and converts each of them to an HTML web page. To use this, just take the output and pipe it directly to ConvertTo-Html. The cmdlet will then return a big string of HTML.
Get-PSDrive | ConvertTo-Html
Since this big string of output isn't too useful, let's capture it to a file using the Out-File cmdlet.
Get-PSDrive | ConvertTo-Html | Out-File -FilePath PSDrives.html
After doing this, check out the PSDrives.html file that this generated.
Invoke-Expression C:\PSDrives.html
Notice it shows nearly the exact same output as the console, only in HTML. What did you expect, anyway? But at this point, we now have the opportunity to make it look better. First, let's limit the number of properties to only those that show up when running Get-PSDrive from the console. Those properties are Name, Used, Provider, Root and CurrentLocation. You can limit the output to these properties by using the Property parameter of ConvertTo-Html.
Get-PSDrive | ConvertTo-Html -Property Name,Used,Provider,Root,CurrentLocation | Out-File -FilePath PSDrives.html
Now it looks like the native console output. We can do much better though. I find it hard to differentiate between the rows since they're so close together. Let's add a border around the rows and columns. To do that requires knowing a little CSS, which I was able to find online. The ConvertTo-Html cmdlet has a Head parameter that allows you to specify the HTML code that will go into the HEAD element. Below, I'm creating a simple CSS style tag to create a border.
$Header = @" <style> TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;} TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;} </style> "@ Get-PSDrive | ConvertTo-Html -Property Name,Used,Provider,Root,CurrentLocation -Head $Header | Out-File -FilePath PSDrives.html
You should now begin to see the possibilities here. Although you'll need to know a little about CSS, you have the ability to make this report look a whole lot better. For my final trick, I'll add a color to the table header values to make them more obvious.
Subscribe to 4sysops newsletter!
$Header = @" <style> TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;} TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;} TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;} </style> "@
Getting the output into HTML opens up a wide range of possibilities. Check out what else ConvertTo-Html can do and, if you're not a web developer, begin searching online for snippets to add to your reports. Remember that even though the resource might not talk about PowerShell, you can use practically any CSS to style these reports in any way you choose.
Very interesting and informative. I didn’t know that Powershell could do that.
It’s a matter not of what PowerShell can do… lol but what it can’t! 🙂
So I am searching all over the web… high and low… I am new to powershell but have a script that does run on a machine and gather a ton of information. I am trying to figure out how to get my output to be html instead of txt. All of my items are listed out as variables and all examples I see using the convert-html all use an item that has tabled items.
so curious if you could help me out. How can I create an html out of just variables. I will show you my code.. well an excerpt as we don’t need it all…
I have some parts of the script have 4 or more rows and some with just 2. so here is an easy one (hopefully so I can get started)
(will create a txt file with Internet Explorer Version: 11.1358.14393.0)
$basedir = “c:\PCReports\”
$today = (Get-Date -UFormat %B-%d-%Y.%H)
$file = (Get-Date -UFormat %B-%d-%Y@%H.%M)
$location = New-Item -Path $basedir -Type Directory -Name $today
$LCSSettingsVerification = “\LCS_Machine_Info_Report-” + $file + “.txt”
$FileSave = $basedir + $today + $LCSSettingsVerification
$myIEVersion = (Get-ItemProperty ‘HKLM:\SOFTWARE\Microsoft\Internet Explorer’).svcVersion
if (!$myIEVersion) {$myIEVersion = (Get-ItemProperty ‘HKLM:\SOFTWARE\Microsoft\Internet Explorer’).Version}
write-output (‘Internet Explorer Version: ‘ + $myIEVersion) |Out-File $FileSave -Append
You could create a here string, put your variables into a html table, then output the here string to a html file
Can you use external style sheets?
$head= <link rel=”stylesheet” href=”styles.css”>
convertto-html -head $head -body $body
Hi,
Very informative, Thanks for sharing the knowledge.
Please help me in clarifying my doubt.
I have a text file (with some data) which I want to convert to HTML with formatting.
This is what i am trying
Get-Content Data.txt | ConvertTo-html | Out-File Data.html.
But I am not getting the desired output. Instead of getting the content of the Data.file in Html format I am getting the path of the Data.txt.
I am new to this, please help me out here.
Regards,
KK
HI ,
how to highlight the last row in a column using the same logic.
Excel Work Sheet to HTML from PowerShell. It creates folder which I don’t want and also inserts Java script in the output also not needed. IF I saved from Excel then is the way I expect it be. How can it be done with Powershell?
I’ve tried:
$Tmp5 = “Test.htm” $xlHtml = 44 # Creates folder Test_files
$ws.SaveAs($Tmp5,$xlHtml)
$xlHtml = 45 # doesn’t create files but wrong format Java etc.
Thanks Adam!
Hi Adam,
Sorry to bug you, I used your teachings and created Table from powershell. Would you please be able to help me with “creating Charts” from those tables as well ? My tables are stored in $Test variable.
Small Example below:
function GetSummary {
$a=Get-MsolDomain | select name,capabilities,status | ConvertTo-Html
# Creating the HTML Page
$Html = @”
<!DOCTYPE html>
<head>
<title>Test Page</title>
<style>
body {
font-family: “Courier New”;
font-size: 10px;
line-height: 2px
}
table, th, td {
white-space: nowrap;
border: 1px solid #25724F;
text-align: center;
border-collapse: collapse;
padding: 2px;
}
th {
background-color:#25724F;
color: white;
}
</style>
</head>
<body>
<br>
Available Domains:
<table class=”4sysops”>
<tr>
<td class=”4sysops-1″ colspan=”6″>$a</td>
</tr>
</table>
“@
Send-Mailmessage -from gautam.verma@domain.com -body $html -BodyAsHtml
}
Hi,
Any idea why it goes from declaring Used and Free in GB to B when you invoke ConvertTo-HTML?
Thanks
@havingacode
This is a hard-coded functionality specific to the “default view”.
The real value of the property is in Bytes.
When you pipe the result from the Get-PSDrive cmdlet to Select-String or Format-Table you get the number in Bytes too.
I really like this article and it works well with PowerShell built in commands but I can't get it to work for output from an EXE file that is called from PowerShell.
In my case I have an application ($EXE) and an output file ($OutputFile) that has a 4 lines output. I cannot get the output to be in HTML though.
This works for me but it output as plain text
Start-Process -FilePath $EXE -Wait -WindowStyle Hidden -RedirectStandardOutput $OutputFile
For some reason ConvertTo-Html will not work for output from an command line exe.
The result of command 'convert-to-html' would depend on the format of your file. You may need to add some parsing/trimming logic to get the file into a delimited format.
$process = Import-Csv C:\Temp\Process.csv
foreach ($proc in $process)
{
$result = Get-Process -Name $proc.Name
Write-Host $proc.name,$result.Handles,$proc.Value
}
For above command I can see the output on screen but how can get the same in CSV or HTML?
How to get html file content as a mail body? or output of Get-PSDrive | ConvertTo-Html -Property Name,Used,Provider,Root,CurrentLocation -Head $Header | Out-File -FilePath PSDrives.html
$body = Get-PSDrive | ConvertTo-Html -Property Name,Used,Provider,Root,CurrentLocation -Head $Header
Send-MailMessage -body $body -bodyasHTML:$true
add other required parameters to Send-MailMessage of course. 🙂
If you are going to execute this:
Invoke-Expression C:\PSDrives.html
Then you need to change this
Get-PSDrive | ConvertTo-Html | Out-File -FilePath PSDrives.html
to this
Get-PSDrive | ConvertTo-Html | Out-File -FilePath C:\PSDrives.html
if you expect it to be in the same place, just my 2 cents.
lovely.. you helped me..!!
Thanks a lot..!!