- Convert To-Html
- Syntax
- Description
- Examples
- Example 1: Create a web page to display the date
- Example 2: Create a web page to display PowerShell aliases
- Example 3: Create a web page to display PowerShell events
- Example 4: Create a web page to display processes
- Example 5: Create a web page to display service objects
- Example 6: Create a web page to display service objects
- Example 7: Create a web table for the current date
- Example 8: Create a web page to display PowerShell events
- Example 9: Create a web page to display specified services
- Example 10: Set the Meta properties and Charset of the HTML
- Example 11: Set the HTML to XHTML Transitional DTD
- Parameters
- -As
- -Body
- -Charset
- -CssUri
- -Fragment
- -Head
- -InputObject
- -Meta
- -PostContent
- -PreContent
- -Property
- -Title
- -Transitional
- Inputs
- Outputs
- Notes
- Related Links
- Feedback
Convert To-Html
Converts .NET objects into HTML that can be displayed in a Web browser.
Syntax
ConvertTo-Html [-InputObject ] [[-Property]
ConvertTo-Html [-InputObject ] [[-Property]
Description
The ConvertTo-Html cmdlet converts .NET objects into HTML that can be displayed in a Web browser. You can use this cmdlet to display the output of a command in a Web page.
You can use the parameters of ConvertTo-Html to select object properties, to specify a table or list format, to specify the HTML page title, to add text before and after the object, and to return only the table or list fragment, instead of a strict DTD page.
When you submit multiple objects to ConvertTo-Html , PowerShell creates the table (or list) based on the properties of the first object that you submit. If the remaining objects do not have one of the specified properties, the property value of that object is an empty cell. If the remaining objects have additional properties, those property values are not included in the file.
Examples
Example 1: Create a web page to display the date
ConvertTo-Html -InputObject (Get-Date)
This command creates an HTML page that displays the properties of the current date. It uses the InputObject parameter to submit the results of a Get-Date command to the ConvertTo-Html cmdlet.
Example 2: Create a web page to display PowerShell aliases
Get-Alias | ConvertTo-Html | Out-File aliases.htm Invoke-Item aliases.htm
This command creates an HTML page that lists the PowerShell aliases in the current console.
The command uses the Get-Alias cmdlet to get the aliases. It uses the pipeline operator ( | ) to send the aliases to the ConvertTo-Html cmdlet, which creates the HTML page. The command also uses the Out-File cmdlet to send the HTML code to the aliases.htm file.
Example 3: Create a web page to display PowerShell events
Get-EventLog -LogName "Windows PowerShell" | ConvertTo-Html | Out-File pslog.htm
This command creates an HTML page called pslog.htm that displays the events in the Windows PowerShell event log on the local computer.
It uses the Get-EventLog cmdlet to get the events in the Windows PowerShell log and then uses the pipeline operator ( | ) to send the events to the ConvertTo-Html cmdlet. The command also uses the Out-File cmdlet to send the HTML code to the pslog.htm file.
The command also uses the Out-File cmdlet to send the HTML code to the pslog.htm file.
Example 4: Create a web page to display processes
Get-Process | ConvertTo-Html -Property Name, Path, Company -Title "Process Information" | Out-File proc.htm Invoke-Item proc.htm
These commands create and open an HTML page that lists the name, path, and company of the processes on the local computer.
The first command uses the Get-Process cmdlet to get objects that represent the processes running on the computer. The command uses the pipeline operator ( | ) to send the process objects to the ConvertTo-Html cmdlet.
The command uses the Property parameter to select three properties of the process objects to be included in the table. The command uses the Title parameter to specify a title for the HTML page. The command also uses the Out-File cmdlet to send the resulting HTML to a file named Proc.htm .
The second command uses the Invoke-Item cmdlet to open the Proc.htm in the default browser.
Example 5: Create a web page to display service objects
Get-Service | ConvertTo-Html -CssUri "test.css" .
This command creates an HTML page of the service objects that the Get-Service cmdlet returns. The command uses the CssUri parameter to specify a cascading style sheet for the HTML page.
The CssUri parameter adds an additional tag to the resulting HTML. The HREF attribute in the tag contains the name of the style sheet.
Example 6: Create a web page to display service objects
Get-Service | ConvertTo-Html -As LIST | Out-File services.htm
This command creates an HTML page of the service objects that the Get-Service cmdlet returns. The command uses the As parameter to specify a list format. The cmdlet Out-File sends the resulting HTML to the Services.htm file.
Example 7: Create a web table for the current date
Get-Date | ConvertTo-Html -Fragment . DisplayHint DateTime Date Day DayOfWeek DayOfYear Hour Kind Millisecond Minute Month Second Ticks TimeOfDay Year DateTime Monday, May 05, 2008 10:40:04 AM 5/5/2008 12:00:00 AM 5 Monday 126 10 Local 123 40 5 4 633455808041237213 10:40:04.12 37213 2008
This command uses ConvertTo-Html to generate an HTML table of the current date. The command uses the Get-Date cmdlet to get the current date. It uses a pipeline operator ( | ) to send the results to the ConvertTo-Html cmdlet.
The ConvertTo-Html command includes the Fragment parameter, which limits the output to an HTML table. As a result, the other elements of an HTML page, such as the and tags, are omitted.
Example 8: Create a web page to display PowerShell events
Get-EventLog -Log "Windows PowerShell" | ConvertTo-Html -Property id, level, task
This command uses the Get-EventLog cmdlet to get events from the Windows PowerShell event log.
It uses a pipeline operator ( | ) to send the events to the ConvertTo-Html cmdlet, which converts the events to HTML format.
The ConvertTo-Html command uses the Property parameter to select only the ID, Level, and Task properties of the event.
Example 9: Create a web page to display specified services
$htmlParams = @< Title = "Windows Services: Server01" Body = Get-Date PreContent = "Generated by Corporate IT
" PostContent = "For details, contact Corporate IT." > Get-Service A* | ConvertTo-Html @htmlParams | Out-File Services.htm Invoke-Item Services.htm
This command creates and opens a Web page that displays the services on the computer that begin with A . It uses the Title, Body, PreContent, and PostContent parameters of ConvertTo-Html to customize the output.
The first part of the command uses the Get-Service cmdlet to get the services on the computer that begin with A . The command uses a pipeline operator ( | ) to send the results to the ConvertTo-Html cmdlet. The command also uses the Out-File cmdlet to send the output to the Services.htm file.
A semicolon ( ; ) ends the first command and starts a second command, which uses the Invoke-Item cmdlet to open the Services.htm file in the default browser.
Example 10: Set the Meta properties and Charset of the HTML
Get-Service | ConvertTo-HTML -Meta @ < refresh=10 author="Author's Name" keywords="PowerShell, HTML, ConvertTo-HTML" >-Charset "UTF-8"
This command creates the HTML for a webpage with the meta tags for refresh, author, and keywords. The charset for the page is set to UTF-8
Example 11: Set the HTML to XHTML Transitional DTD
Get-Service | ConvertTo-HTML -Transitional
This command sets the DOCTYPE of the returned HTML to XHTML Transitional DTD
Parameters
-As
Determines whether the object is formatted as a table or a list. Valid values are Table and List. The default value is Table.
The Table value generates an HTML table that resembles the PowerShell table format. The header row displays the property names. Each table row represents an object and displays the object’s values for each property.
The List value generates a two-column HTML table for each object that resembles the PowerShell list format. The first column displays the property name. The second column displays the property value.
Type: | String |
Accepted values: | Table, List |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Body
Specifies the text to add after the opening tag. By default, there is no text in that position.
Type: | String [ ] |
Position: | 3 |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Charset
Specifies text to add to the opening tag. By default, there is no text in that position.
This parameter was introduced in PowerShell 6.0.
Type: | String |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-CssUri
Specifies the Uniform Resource Identifier (URI) of the cascading style sheet (CSS) that is applied to the HTML file. The URI is included in a style sheet link in the output.
Type: | Uri |
Aliases: | cu, uri |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Fragment
Generates only an HTML table. The , , , and tags are omitted.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Head
Specifies the content of the tag. The default is
Type: | String [ ] |
Position: | 1 |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-InputObject
Specifies the objects to be represented in HTML. Enter a variable that contains the objects or type a command or expression that gets the objects.
If you use this parameter to submit multiple objects, such as all of the services on a computer, ConvertTo-Html creates a table that displays the properties of a collection or of an array of objects. To create a table of the individual objects, use the pipeline operator to pipe the objects to ConvertTo-Html .
Type: | PSObject |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-Meta
Specifies text to add to the opening tag. By default, there is no text in that position.
This parameter was introduced in PowerShell 6.0.
Type: | Hashtable |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-PostContent
Specifies text to add after the closing tag. By default, there is no text in that position.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-PreContent
Specifies text to add before the opening tag. By default, there is no text in that position.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Property
Includes the specified properties of the objects in the HTML. The value of the Property parameter can be a new calculated property. The calculated property can be a script block or a hash table. Valid key-value pairs are:
- Name (or label) — (added in PowerShell 6.x)
- Expression — or
- FormatString —
- Width — — must be greater than 0
- Alignment — value can be Left , Center , or Right
Type: | Object [ ] |
Position: | 0 |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Title
Specifies a title for the HTML file, that is, the text that appears between the tags.
Type: | String |
Position: | 2 |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Transitional
Changes the DOCTYPE to XHTML Transitional DTD, Default DOCTYPE is XHTML Strict DTD.
This parameter was introduced in PowerShell 6.0.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Inputs
You can pipe any object to this cmdlet.
Outputs
This cmdlet returns an array of strings of HTML representing the converted object.
Notes
To use this cmdlet, pipe one or more objects to the cmdlet or use the InputObject parameter to specify the object. When the input consists of multiple objects, the output of these two methods is quite different.
- When you pipe multiple objects to a cmdlet, PowerShell sends the objects to the cmdlet one at a time. As a result, ConvertTo-Html creates a table that displays the individual objects. For example, if you pipe the processes on a computer to ConvertTo-Html , the resulting table displays all of the processes.
- When you use the InputObject parameter to submit multiple objects, ConvertTo-Html receives these objects as a collection or as an array. As a result, it creates a table that displays the array and its properties, not the items in the array. For example, if you use InputObject to submit the processes on a computer to ConvertTo-Html , the resulting table displays an object array and its properties. To comply with the XHTML Strict DTD, the DOCTYPE tag is modified accordingly:
Related Links
Feedback
Submit and view feedback for