Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine Users Accessing a Shared Folder Using PowerShell

I need to determine the users/sessions accessing a shared folder on a Windows XP (SP2) machine using a PowerShell script (v 1.0). This is the information displayed using Computer Management | System Tools | Shared Folders | Sessions. Can anyone give me pointers on how to go about this?

I'm guessing it will require a WMI query, but my initial search online didn't reveal what the query details will be.

Thanks, MagicAndi

like image 868
Tangiest Avatar asked Dec 06 '25 18:12

Tangiest


1 Answers

I came up with the following script:

$computer = "LocalHost"
$namespace = "root\CIMV2"
$userSessions = Get-WmiObject -class Win32_ServerConnection -computername $computer -namespace $namespace

if($userSessions -ne $null)
{
    Write-Host "The following users are connected to your PC: "

    foreach ($userSession in $userSessions)
    {
        $userDetails = [string]::Format("User {0} from machine {1} on share: {2}", $userSession.UserName, $userSession.ComputerName, $userSession.ShareName)
        Write-Host $userDetails
    }    

    Read-Host
}

The following articles were useful:

  • http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/filesfolders/sharedfolders/
  • http://www.computerperformance.co.uk/powershell/powershell_wmi_shares.htm
  • http://www.codeproject.com/KB/cs/NetWorkSpy.aspx?msg=2384830

As always, if you can't find a way to do it in PowerShell, see if someone has done something similar in C#.

like image 135
Tangiest Avatar answered Dec 08 '25 14:12

Tangiest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!