PowerShell Group-Object: Group By, Count and Sum (7 vs 5.1)

PowerShell Group-Object counting orders by state

If you need to count how many orders came from each state, the PowerShell Group-Object cmdlet does it in one line: Import-Csv .\orders.csv | Group-Object -Property State -NoElement. You get one row per state with a Count column. In this tutorial, I’ll show you how PowerShell Group-Object works on one small orders file, from a … Read more

How to Convert TIFF to PDF Using PowerShell

Convert TIFF to PDF with PowerShell

To convert a TIFF to PDF with PowerShell and no extra tools, print each TIFF page to the built-in Microsoft Print to PDF printer with PrintToFile turned on. That writes the PDF silently, with no Save As dialog: Below I check the printer, read a multi-page TIFF, wrap the whole conversion in a function and … Read more

How to Convert a String to a Script Block in PowerShell

Convert a string to a script block in PowerShell

To convert a string to a script block in PowerShell, use [scriptblock]::Create(). Then run it with the call operator &: It’s useful when the code comes from a config file or is built from settings, like a filter. Below I cover parameters, the quoting trap, filters, syntax checks and the security risk you need to … Read more

How to Convert XML to a Table in PowerShell (With Examples)

Convert XML to a table in PowerShell

To convert XML to a table in PowerShell, cast the file to [xml], walk down to the repeating element with dot notation, and pipe those nodes to Format-Table. Each child element and attribute becomes a column: That covers flat files. Real XML has attributes, nested nodes and namespaces, so I’ll also show how to pick … Read more

PowerShell: Convert HTML to Plain Text (Tested in 7 and 5.1)

PowerShell convert HTML to plain text

To convert HTML to plain text in PowerShell, strip the tags with -replace and then decode entities like & with WebUtility.HtmlDecode. For a short snippet, this one line does both: Full pages and email bodies need a bit more care, though. Scripts, styles and line breaks get in the way, and Windows PowerShell 5.1 and … Read more

How to Convert UTF-16 to UTF-8 Using PowerShell

Convert UTF-16 to UTF-8 using PowerShell

To convert a UTF-16 file to UTF-8 with PowerShell, read it with Get-Content and write it back with Set-Content -Encoding utf8: Get-Content recognizes UTF-16 by itself. The catch is the byte order mark: PowerShell 7 writes UTF-8 without one, and Windows PowerShell 5.1 adds it. Below I show the real bytes, a version that behaves … Read more

How to Convert a Log File to HTML Using PowerShell

Convert a log file to HTML using PowerShell

To convert a log file to HTML with PowerShell, split each line into fields, turn the lines into objects and pipe them to ConvertTo-Html. Out-File saves the page: The parsing step is what makes the table readable. Below I parse a sample application log, build a basic page, then a styled report with a summary … Read more