How to Get Yesterday’s Date and Add Days to a Date in PowerShell

Get yesterday's date in PowerShell with AddDays

To get yesterday’s date in PowerShell, subtract one day from today with (Get-Date).AddDays(-1). The same AddDays() method adds days to any date, and a negative number goes back in time: Output: I pinned the examples to fixed 2026 dates so your output matches mine. In a real script, start from Get-Date instead, and the results … Read more

Import-Csv in PowerShell: Read CSV Files and Loop Through Rows

PowerShell Import-Csv reading a CSV file into objects

Import-Csv is the PowerShell cmdlet that reads a CSV file and turns every row into an object. The column headers become property names, so you can sort, filter and loop through the data without parsing text: Output: The file customers.csv holds four US customers with the columns Id, Name, Company, State and Total. I use … Read more

Get-Service in PowerShell: List Running Services and More

Get-Service in PowerShell: list running services and filter them

Get-Service is the PowerShell cmdlet that reads the Windows services on your PC. On its own it lists every service; pipe it to Where-Object to list only the running ones. Here I check a set of standard Windows services: Output: Without -Name, Get-Service returns every service on the PC. I kept the examples to built-in … Read more

Get the Current Directory and Script Path in PowerShell ($PSScriptRoot)

Get the current directory and script path in PowerShell

Get-Location (or $PWD) tells you the current directory in PowerShell. For the PowerShell script path, read $PSScriptRoot for the script’s folder or $PSCommandPath for the full .ps1 path: Those two answers differ more often than you’d think. The current directory is wherever the caller happens to be, while the script path never moves. I ran … Read more