powershellnetwork-share

Find all shares on a domain


Is it possible to find all the network shares on a domain using powershell?

I've tried the following:

Get-WmiObject -class Win32_Share -ComputerName test1

Solution

  • It's possible to run a script that will list all shares. It must be run on Windows Server or a computer that has Microsoft RSAT installed.

    1. Install RSAT (if required).
    2. On each target computer, run winrm quickconfig to allow remote WMI calls.
    3. Run the following powershell script.

    FindAllShares.ps1

    #This must be run on a computer that has the ActiveDirectory module installed (eg. Windows Server)
    #The module can be installed using the RSAT suite from Microsoft. 
    Import-Module ActiveDirectory
    
    #To connect to remote computers, the following needs to be run on them:
    #winrm quickconfig
    
    #Get all the computers on the domain
    $computers = Get-ADComputer -Filter {enabled -eq $true} | select DNSHostName, Name
    
    $skipComputers = @("COMPUTER1", "COMPUTER2") #This is a list of computers to not check
    $skipShares = @("ADMIN$", "IPC$")
    $allShares = @()
    
    #Loop through all of the computers and ask each for their shares
    foreach ($computer in $computers | sort Name)
    {
        #Write-Host $computer.DNSHostName
    
        if ($skipComputers -contains $computer.Name)
        {
            #skip these computers
        } else
        {
            #Write-Host $computer.Name
    
            #Get the shares on this computer
            $shares = Invoke-Command -Computer $computer.DNSHostName -ScriptBlock {Get-WmiObject -class Win32_Share}
    
            foreach ($share in $shares)
            {
                #Write-Host $share.Name
    
                if ($skipShares -contains $share.Name)
                {
                    #skip these shares
                } else
                {
                    $sharePath = "\\$($computer.Name)\$($share.Name)"
                    #Write-Host $sharePath
    
                    $allShares += $sharePath
                }
            }
        }
    }
    
    #Write-host $($allShares -join ";")
    Write-host $($allShares | Out-String)