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

 






    • Related Articles

    • Create a mailbox database in Exchange Server

      Manually provisioning mailbox databases to new users can be arduous. However, administrators can turn to PowerShell scripts to create mailbox database easily. The below script will create a mailbox database in an Exchange Server with the name 'D10'. ...
    • Create a ShortCut for an Exe File

      Creating a shortcut to an application on multiple end user desktops can be a laborious task. However, the process is pretty straightforward when using a PowerShell script. In the following script, provide the path of the EXE file and the exact path ...
    • How to create and manage Microsoft 365 groups with PowerShell

      Introduction: Microsoft 365 Groups is a collaboration feature that allows users to work together and share resources such as calendars, files, and email messages. Microsoft 365 Groups can be created and managed using the Microsoft 365 admin center, ...
    • How to create, copy and remove Active Directory users using PowerShell

      One of the most fundamental and often repeated task by an administrator is to create, copy or remove an Active directory user. Thanks to the New-ADUser cmdlet, it is extremely simple. Below are the PowerShell scripts to create, copy and remove an ...
    • Identifying Active Directory built-in groups

      An Active Directory (AD) environment will contain a set of groups by default when a domain is created. These groups are built-in groups, and they are located in the built-in container. The built-in groups can be used to control access to shared ...