Powershell — Unlock File

Stop-Process -Name explorer -Force Start-Process explorer.exe You know the lock is caused by Explorer (e.g., an image or video file preview stuck open). 2. Finding the Culprit: Identifying the Locking Process PowerShell can't directly break a lock without help, but it can tell you who has the lock. For this, we use the Handle tool from Sysinternals (Microsoft’s official utility suite).

Run this PowerShell one-liner to find which process is locking C:\path\to\your\file.pdf : powershell unlock file

While tools like LockHunter or Process Explorer can solve this, what if you want a native, scriptable solution? Enter . While it lacks a dedicated Unlock-File cmdlet, you can combine several techniques to identify and release locked files. Stop-Process -Name explorer -Force Start-Process explorer

A locked file is blocking a critical automated deployment or build script, and you're willing to risk the owning process failing. 5. A Complete PowerShell Unlock Function Here’s a function that combines detection and safe unlocking: For this, we use the Handle tool from

# Force close all handles to a specific file (use with extreme caution!) & "C:\path\to\handle64.exe" -accepteula -c "C:\path\to\file.pdf" -y The -y flag suppresses confirmation. This immediately rips the lock away from the owning process. The process may crash or lose unsaved data, but the file will be unlocked.

& "C:\path\to\handle64.exe" -accepteula "C:\path\to\your\file.pdf" The output will look like: notepad.exe pid: 8764 type: File C:\path\to\your\file.pdf