Scripting File Shares with PowerShell

Scripting File Shares with Microsoft PowerShellMicrosoft PowerShell List Shares

It’s hard to see how any 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.

Topics for PowerShell Shares

 ♣

Two Mission for this Page

Computer Goal
To list existing shares on the local machine, including those built-in hidden ‘$’ shares. 

PowerShell Goals

  1. Learn how to interrogated the operating system with the WMIs class Win32_Share
  2. Refresher, or reinforcement of the Get-Member technique.
  3. Understand how to filter ‘Types’ of shares.

Example 1a: Enumerate Shares with Win32_Share

I have made this script short and sweet, 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 list shares script
# Author: Guy Thomas

Get-WmiObject Win32_Share

Note 1: With this script PowerShell lists shares on the local machine.  To enumerate file shares on another computer see example 1c.

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 encourage 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 2.0

$Share = gwmi -class Win32_Share | sort type, name
$Share | ft name, description, type, path -auto 

Note 3: 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 4:  It’s not really necessary to create a variable called $Share, moreover, you could write all the code on one line not two.

Note 5:  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 -computerName.
# Author: Guy Thomas
# Version 3.2 March 2008 tested on PowerShell v 2.0

Get-WmiObject Win32_Share -computerName HostName

Note 6:  This is a simple script to highlight the -computerName parameter.  Now that I have alerted you to PowerShell’s -computerName parameter, lookout for other commands that accept this switch.

Note 7:  Just to reinforce the message, you did change HostName?  Didn’t you?

Note 8:  Because PowerShell only needs a unique term, you could shorten -computerName to -computer

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Example 2a: Research Win32_Share with Get-Member

You could regard this diversion with Get-Member as pure 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 2.0

Get-WmiObject Win32_Share | Get-Member

Example 2b: -memberType

# Get-Member and Win32_Share
# Author: Guy Thomas
# Version 2.2 March 2008 tested on PowerShell v 2.0

GWMI Win32_Share | gm -memberType property 

Note 9:  Once again I have used the aliases gwmi, here I have also substituted gm for Get-Member.

Note 10:  The main feature of this example is the -memberType parameter, it filters just properties.  My point is this script does not display Win32_Share’s methods, which we don’t need for listing shares.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Example 3a: Type of Share

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 2.0

gwmi Win32_Share -filter "Type > 1" | Sort-Object name

Note 11:  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

Get-WmiObject Win32_Share | Where-Object {$_.Type -gt 1}

Note 12:  You could filter Get-WmiObject win32_service by appending this command:
| Where {$_.name -NotMatch "__"}

Note 13:  Both Where and Sort should really be written: Where-Object and Sort-Object.

Note 14:  ‘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.

See more PowerShell creating shares »

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

PowerShell Script Files – Get-Acl

Once you have examined the shares you may want to change their Access Control List (permissions).  Fortunately, PowerShell has two cmdlet to assist changing permission with Set-Acl, or else learn more about the permissions with Get-Acl.

See more PowerShell Get-Acl »

Summary of PowerShell Shares

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.

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

 


See more PowerShell share examples including WMI

PowerShell WMI   • Create Win32_Share   • WMI Shares   • Free Permissions Analyzer Tool

Get-Acl  • PowerShell Share Error Codes   • Win32_ComputerSystem  • PowerShell 3.0 CIM

Windows PowerShell  • Free WMI Monitor  • Cacls   • Query   • PowerShell Printer Scripts

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