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.

WindowsFilestroubleshootingpowershellLow risk
Try opening the file with [System.IO.File]::Open(path, 'Open', 'ReadWrite', 'None') in a try block — an exception means it's in use.

01Command

Command
$ 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

RiskLow risk
!Warning: The check opens and closes a real handle — on files an app is actively writing, a rare timing conflict is possible; don't loop this aggressively.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • FileStream Class
    Microsoft Learn · vendor-doc · 2026-09-17
    Evidence: 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) Method
    Microsoft Learn · vendor-doc · 2026-09-17
    Evidence: 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