Search code examples
powershellsmb

Show all Users connected to an Network Drive


I want to see all Users which are connected to a certain Network Drive, and I want to be able to disconnect them (Just disconnect not actually deleting the Network Drive connection) and it has to be done via PowerShell. I scrolled through Google for hours, but can't find anything. I know that it is possible, but just not how. I hope somebody can help me.


Solution

  • I would suggest removing their permissions temprorarily. You can use Revoke-SmbShareAccess (Microsoft Docs) to achieve this. This does not remove the drive from the users machines, but will not allow them to access it.

    For seeing the connected users, you can use the following WMI PowerShell command:

    Get-WmiObject Win32_ServerConnection -ComputerName SERVER01 | Select-Object ShareName,UserName,ComputerName | Where-Object {$_.ShareName -eq "SHARENAME"}
    

    Running this for the server containing the shares will show all users connected.