How To Disconnect Users Accessing a Windows Shared Folder using PowerShell
Until I find a WMI (e.g. based on something like Win32_ServerConnection) or .NET object solution that lets me deal with the list of connections as first class objects rather than parsing the text output, I've come up with this solution based around the OpenFiles utility. I'm fairly concerned that it's fragile and not that obvious to modify for different needs but it seems to be working for my narrow scenario at the moment. I've kept the regex with named captures for the other parts of the output as a reference for if you need to use different parameters for openfiles /disconnect (/A /ID /OP).
Note that repeated use of Select-String is something I don't fully understand but is to do with the fact that this is necessary to get access to the named captures. See this RegEx Named Groups with Select-String posting for details. Happy to change it if someone can tell me how!
$computerName = 'ServerName'
$pattern = '^.+ (?<FileId>\d+) (?<User>[^ ]+).+ (?<OpenFile>C:.+\\SpecificFolder\\.*)$'
$openfiles = openfiles /query /s $computerName /v | Select-String -Pattern $pattern | ForEach-Object {[void]($_.Line -match $pattern); $matches['OpenFile']}
$openfiles | Sort-Object -Unique | ForEach-Object { openfiles /disconnect /s $computerName /a * /op `"$_`"}