Ezine 161 – PowerShell Listing Shares

Ezine 161 – PowerShell Listing Shares

Most servers benefit from file shares as points of contact with users or other servers.  PowerShell has a rich selection of commands to list, create and connect to such shares.

Topics for PowerShell and Shares


This Week’s Secret – How to Uninstall PowerShell

There is one aspect of PowerShell that I have neglected to emphasise in previous ezines, that is when you install PowerShell on Windows Server 2003, it acts like a hotfix for the operating system, rather than a stand-alone program.  This relationship becomes important if you ever have to uninstall PowerShell, the key is to research the PowerShell Hotfix number for your operating system, for example: KB926139. 

The uninstall procedure on XP is different and more traditional.  Call for XP’s Add or Remove Programs wizard and just look amongst the Installed Programs for: Windows PowerShell(TM) 1.0.  As for Vista, the secret is to launch Appwiz.cpl then, crucially, find ‘View Installed Updates’.  This link should lead you to Windows PowerShell(TM) 1.0, from there it’s easy to double click and uninstall.

This Week’s Mission

PowerShell can automate a variety of tasks involving shared folders.  For instance, you may be curious for a list of shares on a particular server.  Alternatively, you may need to create network shares so that users can connect to the server.  There again, you may need a logon script which maps a network drive to a local drive letter.  While you can achieve all the above with PowerShell commands, in this issue I will concentrate just on listing the shares.

For this, and similar missions, PowerShell works hand in glove with WMI (Windows Management Instrumentation) objects, consequently, any knowledge of WMI through VBScript will transfer easily to PowerShell.


As a preliminary to running these scripts, download and install PowerShell and .NetFramework.  Go to Microsoft’s download site and choose the flavour to suit your operating system.

To get these scripts to work, either copy them into the PowerShell interface, or else save the code in notepad and create cmdlet files with a .ps1 extension.  Then call your cmdlet with ./filename.

Example 1 – List or Enumerate Shares

I love the word enumerate.  The word ‘Enumerate’ simply bursts with mathematical gravitas.  In comparison, ‘list’ sounds so weedy and ineffectual.  Well, this script goes and gets the share names and displays them in tabular form.

# PowerShell script to enumerate shared folders
# Author: Guy Thomas
# Version 1.6 March 2008 tested on PowerShell v 1.0

get-WmiObject -class Win32_Share -computer LocalHost

Note 1:  Once the script works, then you can substitute the name of another machine on your network for ‘LocalHost’.  Alternatively, you could remove these two words -computer LocalHost, thereby proving that PowerShell substitutes a default value for this parameter.

Note 2:  The hash symbol # is to REMark out the line with a comment.

Note 3 Intelligence:  PowerShell is a particularly intelligent and forgiving scripting language.  For example it can deduce from its (pole) position that Win32_Share is a class of WMI object.  Thus where Win32_Share is the first parameter, it’s optional to add the code: -class.  What’s more PowerShell has built-in aliases, this is why it can expand ft into format-Table, sort into sort-Object, and of course gwmi into get-WmiObject. 

This cleverness brings with it a special challenge, when we write scripts from scratch should we use the long-winded commands such as format-Table?  Or should we use the alias ft, and risk not knowing what our own script means 6 months down the line?  The answer depends on how comfortable you are with an alias, and if anyone less experienced will need to modify your script.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Example 2 – Greater Efficiency

In Example 2 (below) my main goal is to control the content of PowerShell’s output.  However, I have a subsidiary aim, to introduce PowerShell’s short-hand ways.  The first part is condensed because:
a) I abbreviated get-WmiObject to gwmi.
b) I omitted -class because PowerShell assumes from its first position that Win32_Share is a -class.
c) The extra features of this script are introduced by pipelining, the vertical bar |.  This means that the output of one command, becomes is the input of the next command section.  Incidentally, pipelining could be PowerShell’s signature tune.
d) What this script does, that Example 1 does not do, is firstly sort the shares, and secondly display the ‘Type’ property.

# PowerShell script to enumerate shared folders
# Author: Guy Thomas
# Version 1.6 March 2008 tested on PowerShell v 1.0

gwmi Win32_Share | sort type, name | ft name, type, path -auto

Challenge 1:  Amend the sort command.  For example, sort by path, then name.

Challenge 2: Try a cosmetic change.  After ‘ft name’, choose different properties, for example, description or status.  Footnote, how did I know about these extra properties?  Try this:
gwmi Win32_Share | Get-Member -memberType property

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Example 3 – Learning with get-Help, get-Member and get-Command

It is rare for experts to agree; however, all PowerShell gurus will tell you that the three cmdlets that provide the foundation for learning this language are, get-Help, get-Member and get-Command.

In many ways this section should have come first.  My problem is that I am always restless to show you a script that does real work.  But now the time has come to reflect on how you could have worked out my previous examples for yourself.  The practical point of this script is to show you how to research more WMI objects.  Knowledge of WMI will enable you to automate numerous other server and network tasks.


Copy only the first line into PowerShell, delete the # hash, and only then execute the command.  Repeat the procedure for the other 3 lines.

# get-Command get*
# get-Help get-WmiObject -full
# get-WmiObject -list
# get-WmiObject Win32_Share | get-Member

Note 1:  Curiously, Example 3 will do nothing – unless you remove the #.  Please, please remove one # at a time; for perfection, replace the # as you move down the list of commands.  My point is that the above example is really 4 completely separate learning techniques; they will demonstrate the key cmdlets: get-Command, get-Help and get-Member.

Summary of PowerShell Listing Shares

Here are PowerShell examples to help you take an inventory of shares on a server.  Along the way you can learn the role of -parameters.  If you go the extra mile, you can study get-Command, get-Help and get-Member, thus learn for yourself and thus make me redundant.

If you like this page then please share it with your friends


See More Windows PowerShell Examples of Real-life Tasks

PowerShell Tutorials  • PowerShell Examples  • IpConfig  • Get-Counter  • PowerShell NetSh

Monitor Performance – PowerShell  • PowerShell temp   • PowerShell Delete Temporary files

PowerShell WOL (Wake-on-Lan)  • Services   • Change Computer Description Registry

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.