How to Create an HTML Table in PowerShell

Create an HTML table in PowerShell

To create an HTML table in PowerShell, pipe objects to ConvertTo-Html. Use -Property to pick the columns and -Fragment to get just the table: Output: Each object becomes a row and each property a column. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 and point out where the output differs. Arrays of … Read more

How to Find a Folder by Name in PowerShell

Find a folder by name in PowerShell

To find a folder by name in PowerShell, run Get-ChildItem with -Directory, -Recurse and the folder name in -Filter. The match ignores case: Output: I searched a sample Projects folder so the output stays the same. Every example ran in PowerShell 7.6 and Windows PowerShell 5.1 with the same results. Find folders by partial name … Read more

How to Sort an Array in PowerShell (Strings, Numbers, Objects)

Sort an array in PowerShell

To sort an array in PowerShell, pipe it to Sort-Object. It sorts in ascending order, A to Z and smallest to largest, and ignores case. Add -Descending to reverse it: Output: I ran every example in PowerShell 7.6 and Windows PowerShell 5.1. Most results match, but a few sorts differ between the versions, and I … Read more

PowerShell: Find Files Modified After a Date (or in the Last 24 Hours)

PowerShell find files modified after a date

To find files modified after a date in PowerShell, pipe Get-ChildItem -File -Recurse to Where-Object and compare LastWriteTime with the date: Output: LastWriteTime is the “Date modified” column in File Explorer. I used a sample folder with fixed dates, and I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with the same results. … Read more

How to Get the BIOS Version Using PowerShell

Get BIOS version using PowerShell

To get the BIOS version with PowerShell, run (Get-CimInstance -ClassName Win32_BIOS).SMBIOSBIOSVersion. It works in PowerShell 7 and Windows PowerShell 5.1 and doesn’t need admin rights: Output: I replaced this PC’s real BIOS details with placeholders like Contoso, 1.24.0 and ABC1234XYZ, and blacked them out in the screenshots. Your output will show your own values. Get … Read more

How to Get the Difference Between Two Dates in PowerShell

Get the difference between two dates in PowerShell

To get the difference between two dates in PowerShell, subtract one [datetime] from the other. The result is a TimeSpan, and its TotalDays, TotalHours and TotalMinutes properties give the full difference in each unit: Output: I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with the same results. All dates are fixed so … Read more

PowerShell If, ElseIf and Else Statements (With Examples)

PowerShell if else statements

In PowerShell, an if statement runs a block of code when its condition is true. Add elseif for more conditions and else for everything else. The condition goes in parentheses and the code in braces: Output: PowerShell checks each condition from the top and runs only the first block that’s true. I ran every example … Read more

PowerShell Round to 2 Decimal Places (and Whole Numbers)

PowerShell round to 2 decimal places

To round a number to 2 decimal places in PowerShell, use [Math]::Round($number, 2). To display it with exactly two decimals, use the format operator, ‘{0:N2}’ -f $number, or .ToString(‘0.00′): Output: They look the same here, but they don’t return the same kind of value, and they don’t always round the same way. I ran every … Read more

How to Replace Values in an Array in PowerShell

Replace values in an array in PowerShell

To replace a value in a PowerShell array, assign to its index: $array[1] = ‘new value’. To replace text in every item at once, use -replace on the whole array: Output: Index assignment changes the array in place. The rest of this guide covers replacing across all items, replacing whole items only, and a few … Read more

How to Generate Random Strings in PowerShell

Generate random strings in PowerShell

To generate a random string in PowerShell, turn a list of allowed characters into an array, pick characters with Get-Random in a loop, and join them with -join: Output (yours will differ): That’s fine for test data and file names. For passwords, API keys or tokens, use the secure method further down, since Get-Random isn’t … Read more