Windows PowerShell


Scripting File Shares with PowerShell

Scripting File Shares with Microsoft PowerShellMicrosoft PowerShell List Shares

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.

Topics for PowerShell Shares

 ♣

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 - Short and Sweet

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-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 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.

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 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.

See more Microsoft PowerShell tutorials:

PowerShell Home  • Com  • Shell Application  • Active Directory  • QAD Snap-in  • Get-Member

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

WebThis Site

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.