Create a shared folder

Supply the path of desired folder to be shared and share name as parameter to create a shared folder.

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

 

OutputShare

Leave a Reply