New SmbShare - PowerShell


Overview

The New-SmbShare command creates a new Server Message Block (SMB) share on a Windows Server. It allows users to configure and manage file and printer shares on a network. This command provides a convenient way to create and customize shares to meet specific access, security, and storage requirements.

Syntax

New-SmbShare [-Name] <string> -Path <string> [-Description <string>] [-ReadPermission <AccessRule[]>] [-FullPermission <AccessRule[]>] [-ChangePermission <AccessRule[]>] [-DenyPermission <AccessRule[]>] [-ReadOnly] [-FullControl] [-Change] [-Deny] [-Force] [-Create] [-ErrorAction <ActionPreference>] [-Verbose] [-Confirm] [-WhatIf] [-PassThru]

Options/Flags

  • -Name: Specifies the name of the new SMB share.
  • -Path: Indicates the path to the directory or folder that will be shared.
  • -Description: Optionally provides a description for the share.
  • -ReadPermission: Grants read-only access to specified users or groups. Accepts an array of AccessRule objects.
  • -FullPermission: Grants full control access to specified users or groups. Accepts an array of AccessRule objects.
  • -ChangePermission: Grants change access to specified users or groups. Accepts an array of AccessRule objects.
  • -DenyPermission: Denies access to specified users or groups. Accepts an array of AccessRule objects.
  • -ReadOnly: Grants only read access to the share without allowing changes.
  • -FullControl: Grants full control access to the share.
  • -Change: Grants change access to the share.
  • -Deny: Denies access to the share.
  • -Force: Overwrites existing shares with the same name.
  • -Create: Creates a share even if the specified directory does not exist.
  • -ErrorAction: Specifies the action to be taken in case of errors.
  • -Verbose: Displays additional information about the command’s execution.
  • -Confirm: Prompts the user for confirmation before executing the command.
  • -WhatIf: Displays the actions that would be taken without executing the command.
  • -PassThru: Returns the newly created SMB share object.

Examples

Create a basic SMB share:

New-SmbShare -Name MyShare -Path C:\MyFiles

Create a share with specific access permissions:

$user = New-Object System.Security.Principal.NTAccount("DOMAIN", "username")
New-SmbShare -Name MyShare -Path C:\MyFiles -ReadPermission $user

Create a read-only share:

New-SmbShare -Name MyShare -Path C:\MyFiles -ReadOnly

Common Issues

  • Insufficient permissions: Ensure that the user running the command has sufficient privileges to create shares on the server.
  • Path not found: Verify that the specified path exists and is accessible.
  • Duplicate share name: Avoid creating shares with the same name as existing shares. Use the -Force option to overwrite duplicates.

Integration

Combine New-SmbShare with other commands to automate tasks:

  • Create a share and set NTFS permissions:
$share = New-SmbShare -Name MyShare -Path C:\MyFiles
Set-Acl -Path $share.Path -Acl $share.Acl
  • Create a share with PowerShell script:
$script = "Write-Host 'Hello from SMB share!'"
New-SmbShare -Name MyShare -Path C:\MyShare -FullPermission (New-Object System.Security.Principal.NTAccount("DOMAIN", "username")) -Script $script