Home > PowerShell > Function: New-Share – Creating File Shares Via PowerShell

Function: New-Share – Creating File Shares Via PowerShell


Often we need to create file shares, and this is generally fairly boring. PowerShell can help streamline this process. This function will create the share, but does not set sharing permissions. We’ll cover that later.

This is based partly on How to Use PowerShell to create shared folders in Windows 7. I resolved some minor issues and added the pipeline parameters and some minor error checking, and the description. The script will create the folder if it doesn’t exist, and then share it.

function New-Share {
 param (
  [parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, HelpMessage="No folder name specified")]
    [parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, HelpMessage="No share name specified")]
    [parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$false, HelpMessage="No description specified")]
 # Check for folder; Create it if it doesn't exist
 If (!(Test-Path $FolderName)) {
  New-Item $FolderName -type Directory | Out-Null
 # Check for share; Create it if it doesn't exist
 if (!(Get-WMIObject Win32_share -filter "name='$ShareName'")){
  $Shares.Create($FolderName,$ShareName,0,65535,$Description) | Out-Null
  if (!($error)){
   # the share was created
   return $true
  } else {
   # there was an error
   return $false
 } else {
  # the share already exists
  return $false
} # end function New-Share

And we can then call the function with something like:

New-Share -FolderName "c:\LyncShare" -ShareName "LyncShare" -Description "Used by Lync server to store Address Book files, phone updates, and other important files."

As you can see, it’s pretty straight forward. We’ll cover setting both NTFS and Share permissions soon.


I’ve never been one to really solicit donations for my work. My offerings are created because *I* need to solve a problem, and once I do, it makes sense to offer the results of my work to the public. I mean, let’s face it: I can’t be the only one with that particular issue, right? Quite often, to my surprise, I’m asked why I don’t have a “donate” button so people can donate a few bucks. I’ve never really put much thought into it. But those inquiries are coming more often now, so I’m yielding to them. If you’d like to donate, you can send a few bucks via PayPal at https://www.paypal.me/PatRichard. Money collected from that will go to the costs of my website (hosting and domain names), as well as to my home lab.

Categories: PowerShell Tags: ,
  1. Lorenz
    July 11th, 2012 at 03:51 | #1

    Note that if UAC is active, you get the message that the share is created, but actually it is not.

    Add following code and you know if UAC is bugging you:
    $Result = $Shares.Create($FolderName,$ShareName,0,65535,$Description)
    if ($Result.ReturnValue -eq 2) {
    # Share not create due to UAC
    Return $false

  2. July 14th, 2012 at 06:13 | #2

    Thanks for the help, very useful!

  3. Kamesh
    July 18th, 2012 at 17:58 | #3

    Hi How to assign Everyone permission full control on a shared folder. I dont mean the security permission, i mean the sharing permission.

  4. iconoclasticcreations
    August 30th, 2012 at 23:14 | #4


    Look here at Denis’ code for PowerShell:

    He posts a link to where the answer is. At that link, eddysteenbergen makes a correction to one line:

    $InParams.Access = $null
    $InParams.Access = $sd

    His code worked for me, creating shares on a 2003 Enterprise server from a Windows7 Enterprise workstation.

    NOTE: In my work environment, I’m noticing that shares created programmatically won’t respond to Win32_LogicalShareSecuritySetting requests for share permissions. At home, on Win7 Home Premium, it works fine.

    I’d love some help with that if someone is seeing the same thing.

  1. No trackbacks yet.