Ezine 162 – Creating Shares with PowerShell

Ezine 162 – Creating Shares with PowerShell

The keyword in this ezine is ‘create’.  In the last issue we listed shares, and thus got experience of WMI in general, and Win32_Share in particular.  So now we are ready to turn our focus on the .create method.

Topics for Creating New Shares with PowerShell


This Week’s Secret

Scripts that create objects on YOUR computer make me feel twitchy.  Guy’s subscriber from hell is going to get the wrong end of the stick and create something completely inappropriate.  To let you into another secret, if there is one thing that makes me more nervous than explaining how a script creates, it’s explaining a how a script deletes.

For this reason, this week’s scripts are slightly more cryptic than usual.  There are no instructions for installing PowerShell, or explanation of how to copy and paste the code.  My thinking is that if readers cannot understand how to perform these basic steps, then in all honesty, they should not be given a script that could start deleting objects on their computer.PowerShell Shares Create Script

Mission to Create a Network Share

Our mission is to create a network share from an existing folder.  While we are going to use PowerShell, I am a great believer in having a manual walk-through with Windows Explorer.  The advantage is that you can see the fields, and get a feel for which items are essential for your script.

I am going to divide this network share project into two stages.  In the first stage we are going to research methods to use with the WMI class Win32_Share.  Then in the second stage we will use this knowledge to create a new share with PowerShell.

So, ‘let’s roll, and let’s be careful out there’, as Sargeant Esterhaus of Hill Street Blues would say.

Preamble, List Existing Shares

This script is a useful reminder to see how WMI lists shares.

# Microsoft PowerShell script to list shared folders
# Author: Guy Thomas
# Version 1.5 March 2008 tested on PowerShell v 1.0

get-WmiObject -class Win32_Share | sort type, name

Note 1:  You could use the alias gwmi instead of get-WmiObject.

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

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 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 1: Research Win32_Share’s Methods and Definitions

If you are interested in researching PowerShell’s commands, then please study this example, else go to example 2.

# PowerShell script to Research Win32_Share
# Author: Guy Thomas
# Version 1.2 April 2008 tested on PowerShell v 1.0

$objWMI = [wmiClass] ‘Win32_share’
$objWMI |gm -memberType method |format-List

What this script reveals is that Win32_Share has a method called: ‘Create’.  Furthermore, if you drill down .create has the following six-part definition: Path, Name, Type, MaximumAllowed (connections), Description (comment), Password and Access.

To help make sense of this information, try a manual walk-through whereby you create a network share in Windows Explorer; click on the Tools Menu, then Map Network Drive.  If you try this technique, then you will see why PowerShell requires a Name and Path.  What is less clear is why Win32_Share needs a ‘Type’, just trust me that for our purposes its value is zero.

Example 2: Create a Network Share

We have a slight problem.  I cannot see your c:\ drive.  The example below creates a network share from the temp folder.  However, it would be better, and it would aid your understanding, if you created a folder on your c:\ drive.  Once you have a suitable folder then amended the value for $FolderPath in my script below.  If you are up for another challenge, then edit the value of $ShareName in my script.

# Microsoft PowerShell script to create a network share
# Author: Guy Thomas
## Version 1.6 April 2008 tested on PowerShell v 1.0

$FolderPath = "C:\Temp"
$ShareName = "ChangeMe"
$Type = 0
$objWMI = [wmiClass] ‘Win32_share’
$objWMI.create($FolderPath, $ShareName, $Type)

Note 1:  For the sake of simplicity we are only scripting three parameters for Win32_Share, Path, Name and Type.  The value for each parameter is held by a corresponding variable, thus each is easy to change.

Note 2: I am experimenting with new technique for get-WMIObject:
$objWMI = [wmiClass] ‘Win32_share’. 

Alternatively, you could substitute line 8 with this more traditional technique
$objWMI = get-WMIObject Win32_share.

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:Deleting Shares with PowerShell

To be frank, the most useful job for this script is to delete, the network share so that you can run Example 2 again.

The idea with this script is to list the shares on your machine, then filter that list for the just the share name you wish to delete.  The last line of Example 3 then deletes that one share.

# Microsoft PowerShell script to delete a network share
# Author: Guy Thomas
# Version 2.2 April 2008 tested on PowerShell v 1.0

$ShareName = "Temporary"
$ShareDel = get-WmiObject Win32_Share -filter "Name =’$ShareName’ " $ShareDel.delete()

Note 1:  PowerShell is consistent in that it always uses a method called .delete.  (PowerShell has no .remove, or .erase)  My point is that once you learn .delete here, you can be on the lookout for the same method with other objects, for example file objects.

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

Summary of Creating Shares with PowerShell

Listing computer objects, such as shares, is all well and good, but sooner or later you need a script to create new objects.  My only concern is that you will get the wrong end of the stick, and create something that conflicts with existing objects on your computer.

When the time is ripe and your knowledge is sufficient, then research which features your .create method needs to do its job.  In this instance, you need a name for the share, and of course a path or file location, take it on faith that the type is zero.

So, as Sargeant Esterhaus of Hill Street Blues would say: ‘let’s roll, and let’s be careful out there’.

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.