Time Code With a Stopwatch in PowerShell
Measure elapsed time with the .NET Stopwatch class.
01Command
$ $sw=[Diagnostics.Stopwatch]::StartNew(); Start-Sleep -Seconds 2; $sw.Stop(); $sw.Elapsed02What it does
StartNew creates and starts a high-resolution Stopwatch; call Stop and read Elapsed for a TimeSpan. Unlike Measure-Command, it can be started and stopped multiple times, so it is useful for timing individual sections of a longer script.
03Risk
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- Stopwatch ClassEvidence: High-resolution .NET stopwatch used to measure elapsed time.
Last verified: 2026-09-20 · Category: Developer
06Related solutions
Use the System File Checker to verify every protected Windows system file against known-good copies and automatically replace the damaged ones.
Use Windows Package Manager to install, search, and upgrade thousands of applications from the community repository in one command.
Understand PowerShell's execution policy scopes and set the one that lets you run your own local scripts without gutting the security model.