Windows PowerShell


Create a New Share with PowerShell

Create New Shares with Microsoft PowerShell

Guy always like to get the job done.  In this case the job is persuading PowerShell to create a network share on an existing folder.

Topics for Creating New Shares With PowerShell

 ♣

Mission to Create a Network Share

I am going to divide this project into three stages.  Firstly, enumerate the shares, secondly, research methods for the WMI class Win32_Share.  Finally then create a new share with PowerShell.PowerShell Shares Create Script

Example 1: Preamble, List Existing Shares

I have created this script with just one line and two commands.

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

Example 2: Research Win32_Share Methods and Definitions

If you are interested in background information, study this example, or else go to example 3.

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

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

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

To help make sense of this information try a walk-through whereby you create a network share manually 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 Win32_Share needs a 'Type', just trust me that for our purposes its value is Zero.  Else go ahead, create a Share then run this PowerShell share script.

Example 3: PowerShell Creates the 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 and 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 share
# Author: Guy Thomas
## Version 1.5 March 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 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 wish I knew more about the line:
$objWMI = [wmiClass] 'Win32_share'.  All I can say is that if we generate this object, apply the .create() method, then the script creates the network share as planned.  Alternatively, you could substitute:
$objWMI = get-WMIObject Win32_share.

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and the tools teaches me more about how the system literally operates.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

Summary of PowerShell Create Share

By introducing the .create method, we produce a more advanced Microsoft PowerShell script, than simply listing existing shares.  As you tackle this script take the time to study the parameters.  Perhaps a manual walk-through of creating a share will help your understanding of Win32_Share.

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

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

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

Please report a broken link, or an error.