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")]
[string]$FolderName,
[parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, HelpMessage="No share name specified")]
[string]$ShareName,
[parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$false, HelpMessage="No description specified")]
[string]$Description
)
$error.clear()
# 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
$Shares=[WMICLASS]"WIN32_Share"
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.
Categories: PowerShell
Function, PowerShell




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
}
Thanks for the help, very useful!
Hi How to assign Everyone permission full control on a shared folder. I dont mean the security permission, i mean the sharing permission.
@Kamesh
Look here at Denis’ code for PowerShell:
http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/6aa558a6-f8b4-4cb5-bbb3-76eec9805681/
He posts a link to where the answer is. At that link, eddysteenbergen makes a correction to one line:
$InParams.Access = $null
to
$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.