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

How to Write a Variable to a CSV File in PowerShell

Write a variable to a CSV file in PowerShell

To write a variable to a CSV file in PowerShell, put the values in a [pscustomobject] and pipe it to Export-Csv. Each property becomes a column: Piping a plain string or a hashtable gives you a surprising file instead. Below I show what goes wrong, and how to write strings, arrays, hashtables and loop results … Read more

How to Add a Computer to a Domain Using PowerShell (Add-Computer)

PowerShell 7 console running Add-Computer with -WhatIf to preview joining corp.example.com

To add a computer to a domain with PowerShell, run Add-Computer as an administrator, give it the domain name and an account that’s allowed to join machines, and restart: In this tutorial, I’ll show you how to add a computer to a domain using PowerShell: a safe dry run first, the checks to do before … Read more