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

How to Monitor and Manage CPU Usage Using PowerShell

Monitor and manage CPU usage with PowerShell

To check CPU usage in PowerShell, read the processor counter with Get-Counter. Its CookedValue is the percent of time the CPU was busy: Below I average samples, compare Win32_Processor, turn the Get-Process CPU column into a real percentage, find busy processes, log usage and tame a runaway process, in PowerShell 7 and Windows PowerShell 5.1. … Read more

How to Convert VHD to VHDX Using PowerShell

Convert VHD to VHDX with PowerShell

To convert a VHD to VHDX in PowerShell, shut down the VM and run Convert-VHD on the Hyper-V host. The new format comes from the file extension you give the destination: VHDX supports disks up to 64 TB instead of 2 TB and handles power failures better, so it’s worth converting older disks. Below I … Read more

How to Write JSON to a File in PowerShell (7 and 5.1)

Write JSON to a file in PowerShell

To write JSON to a file in PowerShell, convert the object with ConvertTo-Json and pipe the text to Set-Content. Raise -Depth when the data is nested: That one line hides three traps: cut-off nested data, the wrong file encoding in Windows PowerShell 5.1, and appending that breaks the file. I ran each one in PowerShell … Read more

How to Find Duplicates in an Array in PowerShell

Find duplicates in an array in PowerShell

The fastest way to find duplicates in an array in PowerShell is Group-Object: it groups equal values, and any group with a Count above 1 is a duplicate: I’ll also count each value, make the check case-sensitive, find duplicate positions and duplicate objects, and show why Group-Object crawls in Windows PowerShell 5.1 and what to … Read more