How to Read a CSV File into an Array in PowerShell

Read a CSV file into an array in PowerShell

To read a CSV file into an array in PowerShell, store the output of Import-Csv in a variable. Each row becomes an object, and the variable holds all of them: That one line covers most scripts. Below, I’ll show how to grab a single column, why a one-row file trips up Windows PowerShell 5.1, and … Read more

PowerShell Rename-Item: Rename Files and Folders

PowerShell Rename-Item rename files and folders

PowerShell Rename-Item renames a file, folder or registry key. Give it the current path and the new name, without a folder: Output: That’s all most renames need. Below I cover the mistakes that cause errors, bulk renames with a script block, names with square brackets and renaming folders safely. Every example ran in PowerShell 7 … Read more

How to Convert CSV to Tab-Delimited Using PowerShell

Convert CSV to tab-delimited using PowerShell

To convert a CSV file to tab-delimited with PowerShell, import it and export it again with a tab as the delimiter. In PowerShell, a tab is written “`t”: That puts quotes around every value. Below I show how to drop them in PowerShell 7, a method that works in Windows PowerShell 5.1 too, reading the … Read more

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

PowerShell Write-Warning: Show, Hide and Capture Warnings

PowerShell Write-Warning with examples

PowerShell Write-Warning shows a yellow WARNING: message without stopping your script. Use it for problems the user should know about but that aren’t errors: Warnings go to their own stream, so callers can hide, capture or escalate them. Below I show each option with a small disk check function. Every example gave the same output … Read more

How to Find an Array Element by Value in PowerShell

Find an array element by value in PowerShell

To find an element in a PowerShell array by value, use -contains to check if it’s there, IndexOf() to get its position, or .Where() to return the matching items: They behave differently with upper and lower case, duplicates and objects. Below I show each one on the same city list. The output was identical in … Read more

Find Files Larger Than a Specific Size Using PowerShell

Find files larger than a specific size using PowerShell

To find files larger than a specific size with PowerShell, get the files with Get-ChildItem and filter on the Length property. PowerShell understands KB, MB and GB, so you can write sizes the way you say them: Below I run it on a sample folder, show the size units, find files in a range, include … 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

How to Find Duplicate Files Using PowerShell

Find duplicate files using PowerShell

To find duplicate files with PowerShell, hash every file with Get-FileHash and group the results by hash. Files with the same hash have the same content, whatever their names are: Below I run it on a sample folder, speed it up for big folders, catch same-name files that differ, plan a safe cleanup and compare … Read more