Celeb Glow
general | March 12, 2026

PowerShell Get List Of Folders Shared

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?

10 Answers

Try this:

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

Ref: List Shares in Windows w/ PowerShell

3

Powershell is not able too use SMB protocol in order to list the shares on a remote computer. 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] } }
3

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
2

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
}
1

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

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

1

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
1

Here's a PowerShell one liner that uses net view to enumerate all remote shares a user can see - doesn't mean they have access.

net view | Where {$_ -like "\\*"} | %{$comp = $_.Split(" ")[0]; net view $comp | Where {$_ -like "*Disk*"} | %{$share = $_.Split(" ")[0]; $fullpath = Join-Path $comp $share; $fullpath}}

If you want to see if they have (at least) read access, you can run:

Net view | Where {$_ -like "\\*"} | %{$comp = $_.Split(" ")[0]; net view $comp | Where {$_ -like "*Disk*"} | %{$share = $_.Split(" ")[0]; $fullpath = Join-Path $comp $share; [PSCustomObject]@{Path=$fullpath;HasAccess=$(Test-Path $fullpath -ErrorAction SilentlyContinue)}}}

If you need the output saved you can always pipe it to Export-CSV by throwing the following on after the last bracket:

| Export-CSV "\\path\to\file.csv" -NoTypeInformation

The whole thing is not perfect when net view throws an error but I wrote it based on the comments here and it works pretty well and is helpful for what I need so I thought I'd share. :)

Not a real answer.

Just some tidbits. And several things that did NOT work.

Did not work: get-WmiObject

PS C:\> get-WmiObject -class Win32_Share -computer myserver.example.com

Did not work: get-fileshare

Did not work: get-smbshare

Worked: Cygwin/Mobaxterm

Moba () will access file shares just fine.

"list all available shares for that server" modeBoth these forms work:

$ ls //
a b c
$ ls '\\myserver.example.com\'
a b c

"list the contents of a SPECIFIC share on that server" modeBoth these forms work:

$ ls //
foo bar baz
$ ls '\\myserver.example.com\a'
foo bar baz

Advantages: Moba does NOT differentiate between the "list all available shares for that server" mode and the "list the contents of a SPECIFIC share on that server" mode. -- Which seems to be major obstacle in PowerShell.

Drawbacks:

  • Extra install
  • Not PowerShell. Purely text. Not an objectlike .NET/Windows way of doing this at all.

Worked: net view

Advantages:

  • preinstalled on Windows

Drawbacks: Not PowerShell. Purely text. Not an objectlike .NET/Windows way of doing this at all.

1

I keep hearing "No you can't get a list of shared folders from a server using get-WmiObject. Yet for me, I have been successful in using get-WmiObject to collect a list of all shared folders off my servers from my workstation using the instructions above (see image attached). If I export to a csv, I get a whole lot of other information beside just the names and where the shares live.

shared folders

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy