How to Create a Shared Folder using PowerShell

Create a shared folder

Provisioning and managing shared folders through the native Windows process is a pretty straightforward process. However, things can get quickly out of hand when you need to manage multiple shares across multiple computers. With PowerShell, you can carry out the task.
 
The following script can create a shared folder when provided with the path of the desired folder to be shared and the share name as parameters. Just make sure the folder path exists before running this command.

Script

<#
.SYNOPSIS
This script can be used to create a shared folder.
.DESCRIPTION
This script can be used to create a shared folder.
.EXAMPLE
C:\PS> C:\Script\Shared_Folder.ps1 C:\Users\gautam\Desktop\testfolder share
Creates a shared folder of name share in path C:\Users\gautam\Desktop .
#>
param ( [string]$Path , [string]$Name )
#If folder is available in the given path shares the folder.
#else NewFolder is created and shared.

if((Test-Path $Path) -eq “true”)
{
(Get-WmiObject Win32_Share -List).Create($Path, $Name , 0)
}
else
{
New-Item $Path -Type Directory
(Get-WmiObject Win32_Share -List).Create($Path , $Name , 0)
}
#To show the permissions for the sharedfolder.

$per=Get-WmiObject -Class Win32_Share -Filter “Name=’$Name'”
$per|Get-Acl | Select Path,Owner,Group,AccessToString