How to Convert YAML to JSON in PowerShell

Convert YAML to JSON in PowerShell

To convert YAML to JSON in PowerShell, install the powershell-yaml module, read the file with ConvertFrom-Yaml and pass the result to ConvertTo-Json with a higher -Depth: PowerShell has ConvertTo-Json built in, but it has no YAML parser, so you need the module for real YAML files. Below I cover the module, a no-install option for … Read more

How to Find a Carriage Return in a String in PowerShell

Find a carriage return in a string in PowerShell

To find a carriage return in a string in PowerShell, look for the escape sequence “`r”. Contains() tells you whether it’s there, and IndexOf() tells you where: Carriage returns are invisible, so they cause some of the most confusing bugs, like two strings that look the same but don’t match. Below I make them visible, … Read more

PowerShell Error Variables: $Error, -ErrorVariable, $? and More

PowerShell error variables guide showing $Error and -ErrorVariable counts for SilentlyContinue and Ignore

PowerShell has two main error variables. $Error is an automatic list of every recent error in the session, newest first, and -ErrorVariable saves one command’s errors into a variable you name: In this tutorial, I’ll walk through the PowerShell error variables you’ll actually use: $Error, -ErrorVariable, $? and $LASTEXITCODE. You’ll also see where PowerShell 7 … Read more

How to List Drives in PowerShell (Get-PSDrive, Get-Volume, CIM)

PowerShell Get-Volume listing the C drive and a recovery volume without a drive letter

To list drives in PowerShell, run Get-PSDrive -PSProvider FileSystem. It returns every disk drive letter PowerShell can see, including mapped network drives, with used and free space. For volume details such as file system and health, use Get-Volume: Below you’ll learn how to list drives in PowerShell five ways: Get-PSDrive, Get-Volume, Win32_LogicalDisk, the .NET DriveInfo … Read more

How to List Printers Using PowerShell (Get-Printer and WMI)

PowerShell Get-Printer listing the Microsoft Print to PDF printer in a table

To list printers using PowerShell, run Get-Printer. It returns every printer and printer connection installed on the computer, and you don’t need admin rights. Pipe it to Format-Table to pick the columns: In this post I’ll show you how to list printers using PowerShell with Get-Printer and Win32_Printer, read their status, find network printers, and … Read more

PowerShell Sort-Object: Sort by Property, Number, Date and More

PowerShell Sort-Object examples sorting CSV rows as numbers

PowerShell Sort-Object puts objects in order by one or more property values. Pipe the objects in, name the property, and add -Descending if you want the largest values first: In this tutorial, I’ll show you how to use PowerShell Sort-Object with properties, numbers stored as text, dates, -Unique, -Top and hash tables. Every example below … Read more

How to Find the WWN Number in Windows Using PowerShell

Find the WWN number in Windows using PowerShell

To find the WWN number in Windows with PowerShell, run Get-InitiatorPort and filter for Fibre Channel ports. NodeAddress is the WWNN and PortAddress is the WWPN: Storage admins ask for the WWPN when they zone a switch or map LUNs to a server. Below I format the numbers the way SAN tools expect them, query … Read more

PowerShell Write-Host: Colors, -NoNewline, Streams and Examples

PowerShell Write-Host cmdlet with colored console output

You want a script to show friendly status messages, maybe in green or red, without those messages leaking into the data it returns. That’s exactly the job Write-Host was built for. Quick answer: PowerShell Write-Host prints text straight to the console (the host), with optional -ForegroundColor, -BackgroundColor, -NoNewline and -Separator. It doesn’t send objects down … Read more

Get-ChildItem in PowerShell: List Files, Folders and More

PowerShell Get-ChildItem examples for listing files and folders

Get-ChildItem is the PowerShell cmdlet that lists what’s inside a location: the files and folders in a directory, the keys in a registry hive or the certificates in a store. Point it at a folder and you get the contents back as objects: I reach for it in almost every file script I write. Below … Read more

PowerShell Compare-Object: Compare Files, Folders, CSV and Objects

PowerShell Compare-Object examples for files, folders and CSV data

PowerShell Compare-Object takes two sets of objects and tells you which items exist on only one side. Give it a reference set and a difference set, and it returns the mismatches with an arrow showing where each one came from: I use it constantly for audits: two server lists, two folders, last month’s staff export … Read more