Skip to content

Get-FileShareInfo

Windows: Retrieves information about SMB file shares

#Requires -Version 5.1
#Requires -Modules SmbShare

[CmdletBinding()]
Param
(
    [string]$Name,

    [string]$ComputerName = $env:COMPUTERNAME,

    [switch]$IncludeHidden,

    [pscredential]$Credential
)

Process
{
    try
    {
        $session = $null
        $shareParams = @{
            'ErrorAction' = 'Stop'
        }

        if ($IncludeHidden)
        {
            $shareParams.Add('IncludeHidden', $true)
        }

        if (-not [string]::IsNullOrWhiteSpace($Name))
        {
            $shareParams.Add('Name', $Name)
        }

        if ($ComputerName -ne $env:COMPUTERNAME)
        {
            $sessionParams = @{
                'ComputerName' = $ComputerName
            }
            if ($null -ne $Credential)
            {
                $sessionParams.Add('Credential', $Credential)
            }
            $session = New-CimSession @sessionParams
            $shareParams.Add('CimSession', $session)
        }

        $shares = Get-SmbShare @shareParams

        $results = foreach ($share in $shares)
        {
            [PSCustomObject]@{
                Name         = $share.Name
                Path         = $share.Path
                Description  = $share.Description
                Status       = $share.ShareState
                Type         = $share.ShareType
                CurrentUsers = $share.CurrentUsers
                Scoped       = $share.Scoped
                ComputerName = $ComputerName
            }
        }

        Write-Output ($results | Sort-Object Name)
    }
    catch
    {
        throw
    }
    finally
    {
        if ($null -ne $session)
        {
            Remove-CimSession $session
        }
    }
}

Specifies the name of the share to retrieve. Supports wildcards.

Specifies the name of the computer to query. Defaults to the local computer.

Off

If set, includes hidden (administrative) shares like C$, ADMIN$, etc.

Specifies a PSCredential object for remote connection.

An interactive directory of PowerShell scripts.