Remove locked file (locktype=Shared)
-
03-10-2020 - |
Question
there is a file I want to delete but it is locked. I see with powershell that the lock type is Shared.
>$item = $web.GetListItem('pathtomyfile')
>$item.file.locktype
>Shared
Can I change or remove the locktype with Powershell?
Solution
Following is the powershell script for removing/releasing lock from any document from SharePoint
$web = Get-SPWeb http://intranet.contoso.com
$list = $web.Lists["DocLib"]
$item = $list.GetItemById(2)
$file = $item.File
$userId = $file.LockedByUser.ID
$user = $web.AllUsers.GetByID($userId)
$impSite= New-Object Microsoft.SharePoint.SPSite($web.Url, $user.UserToken);
$impWeb = $impSite.OpenWeb();
$impList = $impWeb.Lists[$list.Title]
$impItem = $impList.GetItemById($item.ID)
$impFile = $impItem.File
$impFile.ReleaseLock($impFile.LockId)
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange