How to Convert UTF-8 to ANSI Using PowerShell

Convert UTF-8 to ANSI using PowerShell

To convert a UTF-8 file to ANSI in PowerShell 7.4 or later, read it as UTF-8 and write it back with -Encoding ansi: Windows PowerShell 5.1 has no ansi value and uses -Encoding Default instead, which means something completely different in PowerShell 7. Below I check the real bytes in both versions and show a … Read more

How to Reverse an Array in PowerShell (In Place or as a Copy)

How to reverse an array in PowerShell

To reverse an array in PowerShell, pass it to the .NET [array]::Reverse() method. It flips the items in place, so the same variable now holds them in the opposite order: To keep the original order too, a negative index range gives you a reversed copy. I ran each method in PowerShell 7 and 5.1. Reverse … Read more

How to Clear an Array in PowerShell (@(), Clear(), ArrayList)

How to clear an array in PowerShell

The quickest way to clear an array in PowerShell is to assign it a new empty array with @(). The variable then holds zero items: That isn’t the only option, and the obvious one, $cities.Clear(), doesn’t do what most people expect. I tested each method in PowerShell 7 and 5.1, and one works only in … Read more

How to Remove Blank Lines from an Array in PowerShell

How to remove blank lines from an array in PowerShell

To remove blank lines from an array in PowerShell, pipe the array to Where-Object and keep only items that aren’t null, empty or whitespace: I tested every example below in PowerShell 7 and Windows PowerShell 5.1, including a popular shortcut that crashes on null items. Remove blank lines from an array with Where-Object This array … Read more

How to Split an Array into Smaller Arrays in PowerShell

Split an array into smaller arrays in PowerShell

PowerShell has no built-in chunk cmdlet, so to split an array into smaller arrays you step through it with a for loop and slice each piece with a range. The comma keeps every chunk as its own array: I’ll also wrap it in a function, show the one-chunk trap, and cover LINQ Chunk, a set … Read more

How to Print an Array in PowerShell (One Line, Loop, Table)

How to print an array in PowerShell

To print an array in PowerShell, type the variable name or pass it to Write-Output, and every item prints on its own line. For a single line, join the items with -join: That covers most cases, but arrays of objects, hashtables and files behave differently. I ran each example below in PowerShell 7 and Windows … Read more

How to Find Invalid Characters in a String Using PowerShell

Find invalid characters in a string using PowerShell

To find invalid characters in a string in PowerShell, compare it against the characters Windows doesn’t allow. For file names, IndexOfAny with GetInvalidFileNameChars returns the position of the first bad one, or -1 if the name is clean: “Invalid” depends on where the text is going, though. Below I check file names, list every bad … Read more

How to Get Array Length in PowerShell (Count vs Length)

How to get the array length in PowerShell

To get the PowerShell array length, read the array’s .Count or .Length property. Both return the number of items, so $cities.Count gives 5 for a five-city array: The tricky part is what happens when a command returns one item, no items, or a collection that isn’t a plain array. I ran every case below in … Read more

How to Add Comments in PowerShell (Single-Line, Block and Help)

How to add comments in PowerShell

To add comments in PowerShell, start a line with # for a single-line comment, or wrap several lines in <# and #> for a block comment. PowerShell skips both when the script runs: Below I go from those basics to comment-based help that Get-Help can read, the nesting trap, #Requires and #region, and comments inside … Read more

How to Get Windows Update History Using PowerShell

Get Windows update history using PowerShell

To get the Windows update history in PowerShell, ask the Windows Update Agent through its Microsoft.Update.Session COM object. QueryHistory returns the same records you see on the Update history page in Settings: Get-HotFix is quicker to type, but it shows only part of the picture. Below I compare both, decode the result codes, find failed … Read more