Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File LockSmith via commandline #22315

Open
nutnutwin opened this issue Nov 27, 2022 · 18 comments
Open

File LockSmith via commandline #22315

nutnutwin opened this issue Nov 27, 2022 · 18 comments
Labels
Help Wanted We encourage anyone to jump in on these and submit a PR. Idea-Enhancement New feature or request on an existing product Product-File Locksmith Refers to the File Locksmith utility

Comments

@nutnutwin
Copy link

Description of the new feature / enhancement

Thank you for creating this feature

I went through documents
https://learn.microsoft.com/en-us/windows/powertoys/file-locksmith
and found no info on how to unlock folder using commandline(CMD)

Scenario when this would be used?

to unlock folder using programming

for example, in LockHunter
https://lockhunter.com/

I can run in

C:\Program Files\LockHunter\LockHunter.exe "C:\Temp"
to unlock folder

Supporting information

No response

@nutnutwin nutnutwin added the Needs-Triage For issues raised to be triaged and prioritized by internal Microsoft teams label Nov 27, 2022
@jdfa
Copy link

jdfa commented Mar 17, 2023

It would be also useful for me.
I think outputting list of processes which is using current file is more important than killing some of them (taskkill can do that).

@pranavpa8788
Copy link

+1 Agreed

@mortl3000
Copy link

+1 would be also useful for me

@xaviercarmo
Copy link

+1, would also like this feature

@joehom0416
Copy link

+1

@crutkas crutkas changed the title Is it possible to run File LockSmith in commandline? File LockSmith via commandline May 17, 2023
@crutkas crutkas added Idea-Enhancement New feature or request on an existing product Product-File Locksmith Refers to the File Locksmith utility Help Wanted We encourage anyone to jump in on these and submit a PR. and removed Needs-Triage For issues raised to be triaged and prioritized by internal Microsoft teams labels May 17, 2023
@simbarras
Copy link

+1

@David-Kyrat
Copy link

David-Kyrat commented Sep 7, 2023

+1
Edit: not needed anymore, I switched to Linux. It was easier. Although I highly support the adding of this kind of power user toys to Windows.

@lwahonen
Copy link

See my comments in #28529

@PZan
Copy link

PZan commented Apr 18, 2024

+1

@cow1337killer3
Copy link

Need this so badly. Sysinternals handle is a piece of junk and doesn't work.

@PolarGoose
Copy link

There are already existing console tools

Don't they do what is asked?

@lwahonen
Copy link

lwahonen commented Sep 7, 2024

The version I implemented has features like "wait until target process closes before returning", which is immensely useful in scripts.

@PolarGoose
Copy link

@lwahonen,

You can do that with a small PowerShell script:

param (
    [Parameter(Mandatory=$true)]
    [string]$PathToTheFileOrFolder
)

$handleInformation = & "$PSScriptRoot/handle64.exe" -accepteula -nobanner -v $PathToTheFileOrFolder
foreach($lockingProcess in ConvertFrom-CSV -Delimiter "," -InputObject $handleInformation) {
    Write-Host "Kill locking process: $lockingProcess"
    Stop-Process -Id $lockingProcess.Pid -Force

    Write-Host "Wait until the process is stopped"
    while (Get-Process -Id $lockingProcess.Pid -ErrorAction SilentlyContinue) {
        Start-Sleep -Seconds 1
    }
}

Also, Sysinternals Handle is more reliable. When run from an admin, it uses a kernel driver, PROCEXP152.SYS, to get the locking information.

@cow1337killer3
Copy link

Also, Sysinternals Handle is more reliable. When run from an admin, it uses a kernel driver, PROCEXP152.SYS, to get the locking information.

Handle is actually very unreliable and often misses handles as well as has false positives. Meanwhile, PowerToys LockSmith actually closes more than necessary, but at least gets the job done.

@PolarGoose
Copy link

@cow1337killer3,

It is strange because Sysinternals Handle uses the same ntdll.dll calls as PowerToys LockSmith in non-admin mode.
Then, I think we need to find reproducible scenarios when they happen. Otherwise, we will chase ghosts.

Do you have any particular scenario that can be reproduced?

@cow1337killer3
Copy link

@PolarGoose

Yea sysinternals just doesn't work for this one use case (after unloading somebody elses' .dll for their Windows context menu handler). I searched for handles on both the file and its parent folder.

Image

I wrote this to programmatically get all the handles. I also tried just using sysinternals directly and searching through the raw text... my function gives the same results.

function handles {
    $target = (get-item $args[0]).fullname.trimend("\")
    $location = (split-path -parent $target).trimend("\")
    
    $handles = ((((& "file-handle" "-a" "-v" | out-string).trim() -replace ' *\r?\n *',"`n") -split '\n\n')[-1]).trim() -split '\n'
    if($handles.count -eq 1 -and $handles[0] -eq "no matching handles found."){ return }
    
    $handles = $handles | convertfrom-csv | ? { 
        $_.type -eq 'file' -and $_.name 
    } | % { 
        $_.name = $_.name.trimend('\') 
        $_
    } | ? { 
        test-path -isvalid -literalpath $_.name 
    } | ? { 
        $_.name -eq $target -or $_.name -eq ($location) 
    }
    
    $handles
}


handles 'shell.dll'

@PolarGoose
Copy link

@cow1337killer3,

Have you tried to use Process Explorer Find->"Find Handle or Dll" ?

@cow1337killer3
Copy link

@PolarGoose Yea that seems to work too. But there's still no command line option to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted We encourage anyone to jump in on these and submit a PR. Idea-Enhancement New feature or request on an existing product Product-File Locksmith Refers to the File Locksmith utility
Projects
Status: No status
Development

No branches or pull requests