How to Convert UTF-8 to ANSI Using PowerShell

Convert UTF-8 to ANSI using PowerShell

To convert a UTF-8 file to ANSI in PowerShell 7.4 or later, read it as UTF-8 and write it back with -Encoding ansi: Windows PowerShell 5.1 has no ansi value and uses -Encoding Default instead, which means something completely different in PowerShell 7. Below I check the real bytes in both versions and show a … Read more

How to Find Invalid Characters in a String Using PowerShell

Find invalid characters in a string using PowerShell

To find invalid characters in a string in PowerShell, compare it against the characters Windows doesn’t allow. For file names, IndexOfAny with GetInvalidFileNameChars returns the position of the first bad one, or -1 if the name is clean: “Invalid” depends on where the text is going, though. Below I check file names, list every bad … 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 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 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

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

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 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

Missing an Argument for Parameter in PowerShell: Causes and Fixes

Fix the PowerShell error Missing an argument for parameter, including Get-Date -Year

PowerShell shows “Missing an argument for parameter” when you type a parameter name that needs a value but give it nothing. For Get-Date -Year, read the property instead: Below are the other causes I reproduced, how to read the error record, and what changes between PowerShell 7 and 5.1. Why Get-Date -Year says Missing an … Read more