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 using the Win32_Share .create method.

Topics for PowerShell Create Share

 ♣

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, 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 February 2010 tested on PowerShell v 2.0 

Get-WmiObject -class Win32_Share | sort type, name

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

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 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 February 2010 tested on PowerShell v 2.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 Shares .Create Method

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 create share script
# Author: Guy Thomas
## Version 1.5 February 2010 tested on PowerShell v 2.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.

See more PowerShell real-life tasks »

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.

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.