How to Get Computer Information Using PowerShell

Get computer information using PowerShell

To get computer information using PowerShell, run Get-ComputerInfo. It returns more than 180 properties about Windows, the hardware and the BIOS. Pick the ones you need with -Property: Output: Notice WindowsProductName says Windows 10 on this Windows 11 PC. It’s a long-standing quirk, and below I show a more reliable way, plus a faster CIM … Read more

How to Create CSV Files with Headers in PowerShell

Create a CSV file with headers in PowerShell using Export-Csv

To create a CSV file with headers in PowerShell, pipe objects to Export-Csv. The header row comes from the property names, so you never type it yourself: Below I cover custom header names, header-only files, appending, empty values and the spots where PowerShell 7 and Windows PowerShell 5.1 write different files. Create a CSV file … Read more

PowerShell Export-Csv: Export Arrays, Hashtables and Tables to CSV

PowerShell Export-Csv examples

To export data to a CSV file in PowerShell, pipe objects to Export-Csv and give it a path. Each object becomes a row, and each property becomes a column: Output: Add -NoTypeInformation if the script might run in Windows PowerShell 5.1. I ran every example in PowerShell 7.6 and 5.1, and I point out each … Read more

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

How to Check Password Expiration Date in PowerShell

Check password expiration date in PowerShell

To check a password expiration date in Active Directory with PowerShell, read the computed msDS-UserPasswordExpiryTimeComputed attribute and convert it to a date: Below I cover the attribute and the math behind it, an expiring-soon report, Microsoft 365 users, and local accounts. My test PC isn’t on a domain, so I ran the date math 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