Check if a File Is Locked (In Use) with PowerShell
Test whether another program is holding a file open by attempting an exclusive .NET File.Open inside try/catch.
01Command
$ try {
$fs = [System.IO.File]::Open('C:\Temp\report.xlsx', 'Open', 'ReadWrite', 'None')
$fs.Close(); 'File is NOT locked'
} catch { 'File IS locked: ' + $_.Exception.Message }02What it does
Requesting the file with FileShare.None only succeeds when nobody else has it open, so the exception message tells you the file is locked, and closing the stream immediately releases your own handle. If it is locked, find the culprit with Resource Monitor's 'CPU → Associated Handles' search, or handle/rundll32 tools. A file you just opened in Excel/Word or a PDF in a browser will naturally test as locked.
03Risk
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- FileStream ClassEvidence: FileStream with FileShare.None throws IOException when the file is already open elsewhere, the mechanism behind the lock test.
- File.Open(String, FileMode, FileAccess, FileShare) MethodEvidence: File.Open with FileShare.None throws IOException when another process holds the file open, which is the basis of the lock test.
Last verified: 2026-09-18 · Category: Files & Folders
06Related solutions
A built-in PowerShell one-liner that answers 'is port 5432/3389/443 actually open through this network?' — no telnet client needed.
Purge cached DNS lookups so Windows re-resolves hostnames from scratch — the fix for 'the new server is live but my browser still hits the old one'.
Diagnose NTFS file system errors and bad sectors on any volume — including the system drive while Windows is running — before committing to a repair.