Had to retrieve information if a filed was locked and who owned it – so wrote the below Powershell function. Ways to improve would perhaps be to provide parsing of multiple-files.

<?Function Get-FileInfo {
Retrieves file-information, such as size, name and locks
Outputs an object with Path, Size, Created on, Last Write Time,
Owner and if the file is locked
Get-FileInfo -File c:\windows\regedit.exe
write-verbose "------------------------"
write-verbose "Start of Get-FileInfo"
write-verbose "Computername:  $($env:computername)"
write-verbose "Username: $($env:USERNAME)"
Write-verbose "Validate file $($file)"

if (Test-Path  $($file))
Write-Verbose "File exists"
throw-error "File does not exist"


#Retrieve file object
$objfile = Get-ChildItem $file

#check file lock
try { [IO.File]::OpenWrite($objfile).close();$lock = $false }
catch {$lock = $true}

#output object
New-Object PSObject -Property @{
Path = $objFile.fullname
Size = "{0:N2} MB" -f ( $objFile.Length / 1mb )
'Created on' = $objFile.CreationTime
'Last Write Time' = $objFile.LastWriteTime
Owner = (Get-Acl $File).Owner
Lock = $lock


write-verbose "End of Get-FileInfo"
write-verbose "------------------------"

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *