param([string] $filename) $string = c:\windows\sysnative\openfiles /QUERY /V | Select-String $filename if($string.line){ $user = %{ $string.line.Split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)[2]; } $fullpath = %{ $string.line.Split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)[-1]; } try{ [IO.File]::OpenWrite("$fullpath").close() $locked = "is not" } catch{ $locked = "is" $result = "File: $fullpath. $user has the file opened and the file $locked locked." $result exit 2 } $result = "File: $fullpath. $user has the file opened and the file $locked locked." $result exit 1 } Else { $result = "File is not Open" $result exit 0 }