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 Find an IP Address from a MAC Address in PowerShell

PowerShell 7 console finding the IP address for a MAC address with Get-NetNeighbor

PowerShell finds an IP address from a MAC address by searching the neighbor cache, which is the ARP table for IPv4. Pass the MAC to Get-NetNeighbor: In this tutorial, I’ll show you how to find an IP from a MAC address in PowerShell with Get-NetNeighbor and arp -a, which MAC formats work, what to do … Read more

How to Check Free Memory in PowerShell (CIM and Get-Counter)

PowerShell 7 console showing free memory, total memory and percent used from Win32_OperatingSystem

Want to check free memory in PowerShell? Query the Win32_OperatingSystem class and read FreePhysicalMemory, which Windows reports in kilobytes: In this tutorial, I’ll show you how to check free memory in PowerShell with Get-CimInstance and Get-Counter, compare installed and usable RAM, see what processes use, and wrap it all in a function. My test machine … Read more

How to Get Dell Service Tag Using PowerShell (7 and 5.1)

Get Dell service tag using PowerShell with Get-CimInstance Win32_BIOS

A Dell service tag is the BIOS serial number, so the quickest way to get the Dell service tag using PowerShell is one CIM query. It works in Windows PowerShell 5.1 and PowerShell 7: In this guide, I’ll show you how to get the Dell service tag using PowerShell on your own PC, why Dell’s … Read more