Detecting locked files

Hi, I'm learning Powershell and came across this script for detecting file locks:

try { [IO.File]::OpenWrite($file).close();$true }
catch {$false}

For my purposes I have attempted to tweak it a bit as I just want the file paths returned if they are locked:

get-childitem -recurse | foreach { try { [io.file]::openwrite($_).close() } catch { $_ } }

I have run this with one file opened in Word as a test but I'm not getting any output.  I'm probably making an obvious mistake so grateful for any pointers, thanks.

August 23rd, 2015 9:44pm

Easier this way:

get-childitem *.DOC*|%{$fi=$_;Try{$fi.OpenWrite()}Catch{$fi}}

Free Windows Admin Tool Kit Click here and download it now
August 23rd, 2015 10:06pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics