How to Get the First and Last Element of an Array in PowerShell

Get the first and last element of an array in PowerShell

To get the first element of an array in PowerShell, use index 0: $array[0]. For the last element, use -1: $array[-1]. Negative numbers count back from the end: Output: Below I cover getting several items at once, Select-Object, and the cases where these give surprising results. I ran every example in PowerShell 7.6 and Windows … Read more

How to Check if an Array Contains a Value in PowerShell

Check if a PowerShell array contains a value

To check if an array contains a value in PowerShell, use the -contains operator. It returns True or False, and it ignores case: Output: That covers the everyday case. Below, I’ll handle the variations people actually search for: case-sensitive checks, partial matches, “does not contain”, several values at once, the index of an item, and … 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

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

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