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

How to Replace a String Before a Character in PowerShell

Replace or remove everything before a character in a PowerShell string

To replace a string before a character in PowerShell, use the -replace operator with a regex that matches from the start of the string up to that character: That pattern stops at the first colon. Below I’ll cover the last-occurrence version, keeping the delimiter, a no-regex option with IndexOf(), -split, special characters, file renames, and … Read more

PowerShell Reference Variables: Pass by Reference with [ref]

PowerShell reference variable: pass a variable by reference to a function with [ref] and .Value

A PowerShell reference variable is a [ref] object that points to another variable. To pass by reference, type the parameter as [ref], call the function with ([ref]$var), and change .Value inside: I’ll show how it works with functions, arrays, hashtables, and .NET methods like TryParse, plus the mistakes I see most and one PowerShell 7 … Read more

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

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

Cannot Convert Value to Type System.String in PowerShell (Fixed)

Fix for the PowerShell error Cannot convert value to type System.String with the hidden array cases tested

PowerShell throws “Cannot convert value to type System.String” when an advanced function’s [string] parameter receives an array instead of one value. Call it once per item, or join the items into one string: In this tutorial, I’ll show you how to reproduce cannot convert value to type system.string, find the hidden array behind it, and … Read more