Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabianneve/35f162919a7084af85a89870238d3741 to your computer and use it in GitHub Desktop.
Save fabianneve/35f162919a7084af85a89870238d3741 to your computer and use it in GitHub Desktop.
Clear short term file lock of a file in a SharePoint 2013+ document library when the file is still open by the user. Replace url, list (List Title) and item ID.
$web = get-spweb "https://domain/sitecollection/site"
$list = $web.Lists["Documents"]
$item = $list.Items.GetItemById(123)
$userId = $item.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);
$impItem.File.ReleaseLock($impItem.File.LockId)
$impWeb.Dispose()
$impSite.Dispose()
$web.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment