How to Get the Current Function Name in PowerShell

Get the current function name in PowerShell with $MyInvocation.MyCommand.Name and Get-PSCallStack

To get the current function name in PowerShell, read $MyInvocation.MyCommand.Name inside the function. It returns the name of the command that’s running right now: I’ll also cover script blocks, the caller’s name, aliases, script paths, logging, and two PowerShell 7 vs 5.1 differences. Get the current function name with $MyInvocation.MyCommand.Name $MyInvocation is an automatic variable … Read more

PowerShell Get-ItemProperty: Read Registry Values and File Properties

PowerShell Get-ItemProperty reading registry values and file properties

PowerShell Get-ItemProperty reads the properties of an item: registry values under a key, or details like LastWriteTime on a file. Give it a path, and optionally a value name: I’ll cover files first, then the registry, where this cmdlet does its real work. Registry reads are read-only, and demo values live in a throwaway test … Read more

How to Check if a String Starts With Text in PowerShell

Check if a string starts with text in PowerShell with StartsWith, -like and -match

The quickest PowerShell string starts with test is the StartsWith() method, which returns True or False: StartsWith() is case-sensitive, so I’ll also show the case-insensitive options, the -like and -match operators, and two traps where PowerShell 7 and Windows PowerShell 5.1 give different answers. Check if a string starts with text using StartsWith() The method … Read more

How to Write to a File in UTF-8 with PowerShell (With or Without BOM)

PowerShell write to file in UTF-8 with or without BOM

To make PowerShell write to a file in UTF-8, add -Encoding utf8 to Out-File, Set-Content, or Add-Content: The catch is the byte order mark (BOM). PowerShell 7 writes that file without a BOM, while Windows PowerShell 5.1 always adds one. I’ll show both results byte by byte, plus the clean ways to get BOM-less UTF-8 … Read more

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 UTF-16 to UTF-8 Using PowerShell

Convert UTF-16 to UTF-8 using PowerShell

To convert a UTF-16 file to UTF-8 with PowerShell, read it with Get-Content and write it back with Set-Content -Encoding utf8: Get-Content recognizes UTF-16 by itself. The catch is the byte order mark: PowerShell 7 writes UTF-8 without one, and Windows PowerShell 5.1 adds it. Below I show the real bytes, a version that behaves … 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 PNG to JPG Using PowerShell

Convert PNG to JPG using PowerShell

To convert PNG to JPG with PowerShell, load the image with System.Drawing and save it in JPEG format. It’s built into Windows, so you don’t need to install anything: That works, but transparent areas turn black and you can’t set the quality. Below I fix both with a reusable function, compare quality settings and convert … Read more