It's hard to see how a network could operate without file shares.
In fact, planning shared folders is one of my first tasks for a new
server. Thanks to the WMI's Win32_Share class, PowerShell
can list or enumerate your existing file shares.
I have made this script just one line with two commands. It will list
all the shares on your local machine.
Instructions:
Copy the text below and paste into your PowerShell command line.
Alternatively copy the code below into a file with a .ps1 file extension.
Then call this file from within by PowerShell using ./filename. See more
on creating and executing cmdlets.
# Simple Microsoft PowerShell share script # Author: Guy Thomas # Version 1.3
March 2008 tested on PowerShell v 1.0
Get-WmiObject Win32_Share
Note 1: With this script PowerShell lists shares
on the local machine. To enumerate file shares on another computer
append this command: - computer hostname.
Note 2: If you need to save the information to a file, then
research the Out-File command.
Example 1b: Enumerate Shares with Extra Control
I believe that you learn more by studying a second way of achieving the same
goal. My idea is that by providing variety you will get better
understanding of what each element does. I hope that this will encourate you
to tinker with my
code and thus create just the script that you need.
# Alternative PowerShell script to enumerate shared
folders # Author: Guy Thomas # Version 2.2 March 2008 tested on PowerShell v 1.0
$Share = gwmi -class Win32_Share | sort type, name
$Share | ft name, description, type, path -auto
Note 1: gwmi is an alias, or shorthand way, of writing
get-WmiObject. The -class parameter is optional, PowerShell can
infer from it's position that Win32_Share is a class of WMI object.
Note 2: It's not really necessary to create a
variable called $Share, moreover, you could write all the code on one line not two.
Note 3: The real point of this example is to gain
extra control. Firstly, we sort the shares by 'Type', secondly we format
with extra properties, such as 'Type'. This leads to the obvious question
of how did I know which properties are available for Win32_Share? The
answer, as we will see below, is get-Member.
Example 1c: How to List Shares on Another Computer
Crucial point: Change Hostname to the name of a real computer on your
network. If all else fails, try localhost.
# PowerShell script to list shared
folders with -computer. # Author: Guy Thomas # Version 3.2 March 2008 tested on PowerShell v 1.0
get-WmiObject Win32_Share -computer HostName
Note 1: This is a simple script to highlight the
-computer parameter. Now that I have alerted you to PowerShell's
-computer parameter, lookout for other commands that accept this switch.
Note 2: Just to reinforce the message, you did change
HostName? Didn't you?
˚
Example 2a: Research with Get-Member
You could regard this diversion with get-Member as pure PowerShell research
for properties of the Win32_Share class.
# Get-Member to research Win32_Share # Author: Guy Thomas # Version 1.2 March 2008 tested on PowerShell v 1.0
# Get-Member and Win32_Share # Author: Guy Thomas # Version
2.2 March 2008 tested on PowerShell v 1.0
gwmi Win32_Share |gm
-memberType property
Note 1: Once again I have used the aliases
gwmi, here I have also substituted gm for
get-Member.
Note 2: The main feature of this example is the -memberType
switch which filters just properties. My point is that this script does
not display Win32_Share's methods, which we don't need for listing shares.
One reason why I researched the Win32_Share properties was to check on the 'Type'
property; I wanted to use this property to filter the shares. What I
had in mind was to display only built-in administrative shares.
The PowerShell logic is that shares have a numeric value for 'Type'.
Furthermore, built-in volume and Administrative shares have high values for
'Type', they begin with numbers such as 21474836xx.
# Microsoft PowerShell Filter Win32_Share # Author: Guy Thomas # Version
1.4 March 2008 tested on PowerShell v 1.0
gwmi Win32_Share -filter
"Type > 1" |sort name
Note1: The key parameter or switch is -filter; a
fact which you could research further with: get-help get-WmiObject.
Example 3b: Where Share Type is greater than 1
I admire the simplicity of the -filter, but I prefer the standard PowerShell
'where-Object' clause.
# Filter Win32_Share # Author: Guy Thomas # Version
1.4 March 2008 tested on PowerShell v 1.0
gwmi Win32_Share |
Where-Object {$_.Type -gt 1}
Note 1: Both Where and Sort are really
Where-Object and Sort-Object
Note 2: 'Where' takes {curly brackets} and also makes
use of the $_. which translates to: 'this pipeline', or to be precise, the
'Type' object in this pipeline.
Summary of PowerShell and List Shared Folders
Our mission on this
page is to employ Microsoft PowerShell to list shares on the local machine. We have
also applied PowerShell techniques such as get-Member to research useful
properties such as 'Type' of share. The we have applied this knowledge to
create scripts which filter the list of file shares.
Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.