How to Read Log Files with PowerShell (Get-Content, Select-String)

Read log files with PowerShell using Get-Content and Select-String

To read log files with PowerShell, use Get-Content for the lines and Select-String to find the ones that matter: The first line shows the newest 20 entries, and the second lists every error with its file name and line number. I tested each step below on sample Contoso logs in PowerShell 7 and Windows PowerShell … Read more

How to Find All Files with a Specific Extension Using PowerShell

Find all files with a specific extension using PowerShell

To find all files with a specific extension using PowerShell, run Get-ChildItem with -Filter, -Recurse and -File: That walks C:\Data and every subfolder and returns only .pdf files. Below, I test each variation on a small sample folder in PowerShell 7 and Windows PowerShell 5.1, including one 5.1 quirk that quietly adds the wrong files. … Read more

Fix ‘Install-Module Is Not Recognized’ in PowerShell and CMD

Fix Install-Module is not recognized as an internal or external command

“‘Install-Module’ is not recognized as an internal or external command” means you typed the command into Command Prompt, not PowerShell. Open PowerShell and run it there, or call it from cmd like this: If PowerShell itself says the term isn’t recognized, the cause is different. I reproduced each one below in PowerShell 7 and Windows … 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 Restart a Windows Service Using PowerShell (7 and 5.1)

Restart a Windows service using PowerShell

To restart a Windows service using PowerShell, open PowerShell as administrator and run Restart-Service with the service name: Restart-Service stops the service and starts it again in one step. It works the same in PowerShell 7 and Windows PowerShell 5.1, but remote restarts and services with dependents need a little more care. Find the service … Read more

How to Set a Service to Automatic Using PowerShell (7 and 5.1)

Set a service to Automatic using PowerShell

To set a service to automatic using PowerShell, open PowerShell as administrator and run Set-Service with the service name and -StartupType Automatic: That one line works in PowerShell 7 and Windows PowerShell 5.1. Delayed start, checking the result and remote servers don’t work the same in both, and that’s where most scripts break. Find the … Read more

How to Create Local Users in Windows Using PowerShell

Create local users in Windows using PowerShell

To create a local user in Windows with PowerShell, open PowerShell as administrator and run New-LocalUser with a name and a secure password: Then add the account to a group with Add-LocalGroupMember. Below I cover options, name rules, bulk creation from a CSV file, and the Access denied error. Create a local user account with … Read more

How to Throw an Exception with a Message in PowerShell

PowerShell throw exception with a custom message

To throw an exception with a message in PowerShell, put your text after the throw keyword. It stops the script right there, and a try/catch block can read the text back from $_.Exception.Message: A plain string is enough most of the time. Below I’ll show what the error looks like in PowerShell 7 and Windows … Read more

How to Split a String and Get the Second Element in PowerShell

Split a string and get the second element in PowerShell

To split a string and get the second element in PowerShell, split it with -split and read index [1], because arrays start at 0: The parentheses matter: they finish the split before PowerShell applies the index. Below I cover the .Split() method, extra spaces, special delimiters, missing values and whole files, with the spots where … 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