14

I am trying to get a list of folders that are shared on a file share. At the moment I have two test folders:

\\MYPC\Test1

\\MYPC\Test2

This is the code I have at the moment:

$FileServer = Read-Host "Enter file server to search"
$FolderList = Get-ChildItem -Path $FileServer

Write-Host $FolderList

But this comes up with "cannot find the path". I can see examples of how to do this for \\Server\Share as the directory, but is it possible to just search the \\Server?

21

Try this:

get-WmiObject -class Win32_Share -computer dc1.krypted.com

Ref: List Shares in Windows w/ PowerShell

  • 4
    This would require WMI rights on the destination machine, which isn't a particularly portable solution. – Mark Henderson Jun 17 '14 at 2:52
  • 2
    plus, it would require RPC communications, which are likely to be firewalled in many configurations even where generic SMB is permitted. Admittedly, net view would not return hidden shares. – syneticon-dj Jun 17 '14 at 9:10
13

There's only one way of enumerating shares remotely from the command line that I know of, and thats with net view:

C:\Users\mark.henderson>net view \\enetsqnap01
Shared resources at \\enetsqnap01



Share name             Type  Used as  Comment

-------------------------------------------------------------------------------
Backups                Disk
CallRecordings         Disk
Download               Disk           System default share
home                   Disk           Home
homes                  Disk           System default share
Installs               Disk
Justin                 Disk           Copy of files from Justin laptop
michael                Disk
Multimedia             Disk           System default share
Network Recycle Bin 1  Disk           [RAID5 Disk Volume: Drive 1 2 3 4]
Public                 Disk           System default share
Qsync                  Disk           Qsync
Recordings             Disk           System default share
Sales                  Disk           Sales Documents
SalesMechanix          Disk
Server2012             Disk           Windows Server 2012 Install Media
Usb                    Disk           System default share
VMWareTemplates        Disk
Web                    Disk           System default share
The command completed successfully.

This is not particularly parsable on its own, but, you can throw it into an array to process the data line by line:

$sharedFolders = (NET.EXE VIEW \\enetsqnap01) 

You now have an array, and starting at $sharedFolders[7] you have your shares. You could then split on something like a double space - unlikely to appear in a share name itself, and should work unless your share name is very long, only leaving a single space between the share name and the type field:

$sharedFolders[7].split('  ')[0]
Backups

You could process these by using a ForEach and some conditional logic. It wouldn't be perfect, but it should work for most use cases.

For brevity, to just output the filenames to the console:

(net view \\enetsqnap01) | % { if($_.IndexOf(' Disk ') -gt 0){ $_.Split('  ')[0] } }
  • FYI: I added a helper function to wrap up the call and break down the text output in a semi-intelligent way... hopefully it makes sense / helps some people out there. – JohnLBevan May 20 '16 at 14:46
  • 1
    @JohnLBevan I don't see it here. Maybe the edit got rejected? If you submit it again I'll see if I can review it in time before someone else gets to it. – Mark Henderson May 20 '16 at 21:10
  • Thanks @Mark Henderson. From the review notes (superuser.com/review/suggested-edits/535793) it seems people would prefer me to post my code in a separate answer, so I've posted here: superuser.com/a/1079174/156700 . Hope this is useful to others. Thanks again for your solution. – JohnLBevan May 20 '16 at 22:22
6

If you want to find the shares of the local machine you can just do Get-SmbShare:

> Get-SmbShare

Name                          ScopeName                     Path                          Description
----                          ---------                     ----                          -----------
ADMIN$                        *                             C:\WINDOWS                    Remote Admin
C$                            *                             C:\                           Default share
3

Expanding on Mark Henderson's answer:

$Servers = ( Get-ADComputer -Filter { DNSHostName -Like '*' }  | Select -Expand Name )
foreach ($Server in $Servers)
{
    (net view $Server) | % { if($_.IndexOf(' Disk ') -gt 0){ $_.Split('      ')[0] } } | out-file C:\file_shares\$Server.txt
}
  • 2
    Can you explain what your expansions do? – bertieb Sep 15 '15 at 8:01
3

Thanks to Mark Henderson for his solution. I've added a wrapper function to help make this function call more PowerShell friendly. I've used a different approach to breaking up the data (more complex, not better); that can easily be switched around based on preference.

clear-host
function Get-SharedFolder {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$ComputerName 
        ,
        [Parameter(Mandatory = $false)]
        [switch]$GetItem
        ,
        [Parameter(Mandatory = $false)]
        [string[]]$ColumnHeadings = @('Share name','Type','Used as','Comment')  #I suspect these differ depending on OS language?  Therefore made customisable
        ,
        [Parameter(Mandatory = $false)]
        [string]$ShareName = 'Share name' #tell us which of the properties relates to the share name
        #,
        #[Parameter(Mandatory = $false)]
        #[string[]]$Types = @('Disk') # again, likely differs with language.  Also there may be other types to include?
    )
    begin {
        [psobject[]]$Splitter = $ColumnHeadings | %{
            $ColumnHeading = $_
            $obj = new-object -TypeName PSObject -Property @{
                Name = $ColumnHeading
                StartIndex = 0
                Length = 0
            }
            $obj | Add-Member -Name Initialise -MemberType ScriptMethod {
                param([string]$header)
                process {
                    $_.StartIndex = $header.indexOf($_.Name)
                    $_.Length = ($header -replace ".*($($_.Name)\s*).*",'$1').Length
                }
            }
            $obj | Add-Member -Name GetValue -MemberType ScriptMethod {
                param([string]$line)
                process {
                    $line -replace ".{$($_.StartIndex)}(.{$($_.Length)}).*",'$1'
                }
            }
            $obj | Add-Member -Name Process -MemberType ScriptMethod {
                param([psobject]$obj,[string]$line)
                process {
                    $obj | Add-Member -Name $_.Name -MemberType NoteProperty -Value ($_.GetValue($line))
                }
            }
            $obj
        }
    }
    process {
        [string[]]$output = (NET.EXE VIEW $ComputerName)
        [string]$headers = $output[4] #find the data's heading row
        $output = $output[7..($output.Length-3)] #keep only the data rows
        $Splitter | %{$_.Initialise($headers)}
        foreach($line in $output) { 
            [psobject]$result = new-object -TypeName PSObject -Property @{ComputerName=$ComputerName;}
            $Splitter | %{$_.Process($result,$line)}
            $result | Add-Member '_ShareNameColumnName' -MemberType NoteProperty -Value $ShareName
            $result | Add-Member 'Path' -MemberType ScriptProperty -Value {("\\{0}\{1}" -f $this.ComputerName,$this."$($this._ShareNameColumnName)")}
            $result | Add-Member 'Item' -MemberType ScriptProperty -Value {Get-Item ($this.Path)}
            $result | Add-Member -MemberType MemberSet -Name PSStandardMembers -Value ([System.Management.Automation.PSMemberInfo[]]@(New-Object System.Management.Automation.PSPropertySet(‘DefaultDisplayPropertySet’,[string[]](@('ComputerName','Path') + $ColumnHeadings))))
            $result
        }
    }
}

[string[]]$myServers = 'myServer1','myServer2' #amend this line to get the servers you're interested in
[psobject[]]$shares = $myServers | Get-SharedFolder
write-host 'List of Shares' -ForegroundColor Cyan
$shares  | ft -AutoSize
write-host 'Shares as Get-Item output' -ForegroundColor Cyan
$shares  | select -expand Item
0

Windows Resource Kit tool: rmtshare.

Either run under id with administrator permissions on the remote server or make an ipc$ connection to remote server.

rmtshare \\servername
  • Can you expand your answer a bit more to include the steps needed to solve the problem? – Cfinley Apr 10 '15 at 17:59
0

On Windows 8 or higher and Windows Server 2012 or higher, you can use Get-SmbShare from the SmbShare module.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.