PowerShell Cannot Convert Value to Type System.Int32 (Fixed)

Fix for the PowerShell error Cannot convert value to type System.Int32 with cast, -as and TryParse results

My restock script died on the first row of a Columbus store’s inventory CSV with “Cannot convert value to type System.Int32”. It means PowerShell tried to turn a value into a whole number and couldn’t. The quickest safe fix is to test the conversion first: In this tutorial, I’ll show you what causes cannot convert … 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 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

How to Read Log Files with PowerShell (Get-Content, Select-String)

Read log files with PowerShell using Get-Content and Select-String

To read log files with PowerShell, use Get-Content for the lines and Select-String to find the ones that matter: The first line shows the newest 20 entries, and the second lists every error with its file name and line number. I tested each step below on sample Contoso logs in PowerShell 7 and Windows PowerShell … Read more

How to Find All Files with a Specific Extension Using PowerShell

Find all files with a specific extension using PowerShell

To find all files with a specific extension using PowerShell, run Get-ChildItem with -Filter, -Recurse and -File: That walks C:\Data and every subfolder and returns only .pdf files. Below, I test each variation on a small sample folder in PowerShell 7 and Windows PowerShell 5.1, including one 5.1 quirk that quietly adds the wrong files. … Read more