How to Find the Most Recent File in a Directory with PowerShell

Find the most recent file in a folder with PowerShell

To find the most recent file in a directory with PowerShell, list the files with Get-ChildItem, sort them by LastWriteTime from newest to oldest, and keep the first one: Output: LastWriteTime is the date Windows shows as Date modified. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 against the same sample folder, … Read more

How to Find and Remove Stale Computer Objects in Active Directory with PowerShell?

If you manage Active Directory, you’ve probably noticed that computer accounts tend to pile up over time. Machines get replaced, users switch laptops, or systems are decommissioned — but those old computer objects? They often stick around in AD. These stale computer accounts aren’t just clutter. They can create security vulnerabilities (imagine an attacker reactivating … Read more

How to Break Out of a ForEach Loop in PowerShell (and Skip Items)

Break out of a foreach loop in PowerShell

To break out of a foreach loop in PowerShell, use the break keyword. The loop stops right away and the script carries on with the next line after the loop: Output: To skip just one item and move on to the next, use continue instead. Both behave differently in ForEach-Object and switch, which I cover … Read more

How to Find Unquoted Service Paths with PowerShell?

If you manage Windows systems—whether you’re a sysadmin, security analyst, or just someone who wants to keep their environment secure—there’s a surprisingly common vulnerability hiding in plain sight: unquoted service paths. In this tutorial, I will explain how to find unquoted service paths with PowerShell using different methods. But let us first understand why it … Read more

Why Does Windows PowerShell Keep Popping Up? (Find and Fix It)

Why Windows PowerShell keeps popping up

Windows PowerShell keeps popping up because something on your PC is starting it: usually a scheduled task, a startup entry, or an app or management script running in the background. Occasionally it’s malware. The fix is to find what launches it. Below I show three read-only checks that do exactly that, then how to stop … Read more

How to Find the Location of an Executable in PowerShell

Find the location of an executable in PowerShell

To find the location of an executable in PowerShell, run Get-Command and read its Source property. It searches the folders in your PATH, just like running the program does: Output: Below I look up several programs at once, list every copy with -All and where.exe, and find where a running program lives. I used only … Read more

How to Find Ports in Use using PowerShell?

Find Ports in Use using PowerShell

Recently, while troubleshooting network connectivity issues, I needed to know which ports are in use. You can easily do this using Microsoft PowerShell. In this tutorial, I will explain to you how to find ports in use using PowerShell using various methods. What Are Ports? A port is a communication endpoint used by networked applications … Read more

PowerShell Find IP from MAC Address

PowerShell Find IP from MAC Address

Do you want to find an IP address from a MAC address using PowerShell? I will explain in detail how to find it with examples. You will learn how to map MAC addresses to IP addresses on your local network using PowerShell. What Are MAC and IP Addresses Let me explain first the difference between … Read more

How to Get the Last Reboot Time Using PowerShell

Get the last reboot time with PowerShell

To get the last reboot time with PowerShell, read LastBootUpTime from Win32_OperatingSystem. Subtract it from the current time to get the uptime: Output: Get-CimInstance returns a normal DateTime, so no conversion is needed. Below I show the shorter PowerShell 7 cmdlet, the reboot history from the event log, remote PCs and one Fast Startup catch. … Read more