PowerShell WMI

Guy recommends:
Free config generator

Solarwinds Config Generator

This CG will put you in charge of controlling changes to network routers and other SNMP devices.

Download your free Config Generator



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

 ♣

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 share script
# Author: Guy Thomas
# Version 1.3 March 2008 tested on PowerShell v 2.0

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

Get-WmiObject Win32_Share -computerName HostName

Note 1:  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 2:  Just to reinforce the message, you did change HostName?  Didn't you?

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

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

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

See more PowerShell share examples including WMI

PowerShell WMI   • Win32_Share   • WMI Shares   • Win32_ComputerSystem   • Query

Get-Acl   • Set-Acl   • PowerShell Create Share   • PowerShell Error Codes

Please write in 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.

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: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

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

Please report a broken link, or an error.