PowerShell: Manage Windows 10 network and folder shares

PowerShell , just like net use , net share , allows you to manage your Windows 10 network and folder shares .
Different cmdlets and commands exist to list, create and delete network shares as well as manage permissions on network shares.

This article guides you to manage your network shares in Powershell.

Contents

  • 1Introduction to the SmbShare Powershell cmdlet
  • 2PowerShell: manage Windows 10 network and folder shares
    • 1PSSession and remote session
    • 2List and display network shares
    • 3Create a network share
    • 4List the permissions of a share
    • 5Add, Modify, delete permissions for a network share
    • 6Deny access to a folder share to a group or user
    • 7Modify the properties of a network share
    • 8Delete a network share
    • 9Create network drive or map network drive
  • 3Find the solution on the help forum

INTRODUCTION TO THE SMBSHARE POWERSHELL CMDLET

Powershell cmdlet Description
Get-SmbShare List network shares
New-SmbShare Create a new network share
Remove-SmbShare Delete a network share
Grant-SmbShareAccess Change permissions for a network share
Revoke-SmbShareAccess Remove permissions from a network share
Set-SmbShare Change the properties of a share
Get-SmbShareAccess List the permissions of a share
New-SmbMapping Map a network drive

SmbShare Powershell cmdlets

The list of other SmbShare cmdlets in Microsoft documentation: List SmbShare cmdlet

In general, when placing an order, a confirmation is made with Yes or no.
You can override the confirmation with the -Force option .
To target a share and work on it, we have the -Name option followed by the name of the share.

POWERSHELL: MANAGE WINDOWS 10 NETWORK AND FOLDER SHARES

PSSESSION AND REMOTE SESSION

With Powershell, you can very easily list the local network resources but you can also work on a remote PC thanks to PSSession .

After opening PowerShell, we start a session from a remote PC with the following command:

Enter-PSSession -ComputerName NomDuPC

LIST AND DISPLAY NETWORK SHARES

In Powershell, we therefore use the get-SmbShare command in order to obtain the list of network shares.

Just enter the command and you will have the list of shares with name, full path and description.

Get-SmbShare

Note also that there is this command:

get-WmiObject -class Win32_Share

See also this article:

How to list and find a shared folder on Windows 10

In order to get the properties of a particular share, we use Select *.
So to get the properties of the share:

Get-SmbShare -Name share | select *

CREATE A NETWORK SHARE

To create a network share in Powershell, we use the New-SmbShare command .

To create a Movies share on the D: \ Movies folder:

New-SmbShare -Name “Films” -Path “D:\Films”

Here are the options for setting permissions:

  1. -ReadAccess: gives read access
  2. -ChangeAccess: gives permissions for modifications
  3. -FullAccess: attribute full control
  4. -NoAccess: no access

For example to create a folder share with permissions in modifications for the Users group and full control over the Administrators group .

New-SmbShare -Name “Film” -Path “D:\Film” -ChangeAccess “Utilisateurs” -FullAccess “Administrateurs”

LIST THE PERMISSIONS OF A SHARE

It is then possible to display the permissions of a share with Get-SmbShareAccess :

Get-SmbShareAccess NomDuPartage

Here we see that the share network share is readable for everyone .

In this other example, Everyone is denied full control , so they do not have access to the share of this folder, but the user Mak has permissions for modifications .

Finally to display the permissions in list with Format-List :

Get-SmbShareAccess share | Format-List

ADD, MODIFY, DELETE PERMISSIONS FOR A NETWORK SHARE

PowerShell offers all the commands to add, modify, or remove permissions for a share.

To modify an existing share, we use Grant-SmbShareAccess .
Here we target the movie network share and the MSI \ MaK user account to give full access.
We see that we are moving from modified permissions to full control.

Grant-SmbShareAccess -Name film -AccountName MSI\MaK -AccessRight Full -Force

PowerShell allows you to manipulate share permissions.
When you need to modify an authorization, you must first withdraw it with Revoke-SmbShareAccess .
Here we remove all authorizations for the MaK user.

Revoke-SmbShareAccess -Name film -AccountName MSI\MaK -Force

Finally we use Grant-SmbShareAccess to add new permissions to a share.
We specify the users or groups that we want to have access to the share as well as the level of access they will have.

Grant-SmbShareAccess -Name film -AccountName MSI\MAK -AccessRight Change

DENY ACCESS TO A FOLDER SHARE TO A GROUP OR USER

You might need to deny specific users or groups access to a file share for security reasons. All Deny permissions override all Allow permissions. So even if users have read or edit permissions on the share, if you specifically deny them permission to that share or if they are in a denied group, they won’t be able to access that share.

To deny a user or group’s access to the file share, we will use the Block-SmbShareAccess command .
In this case, we refuse the MaK user of the PC MSI:

Block-SmbShareAccess -Name film -AccountName MSI\Mak -Force

Finally you must use the UnBlock-SmbShareAccess command to authorize access:

UnBlock-SmbShareAccess -Name film -AccountName MSI\Mak -Force

MODIFY THE PROPERTIES OF A NETWORK SHARE

Set-SmbShare is a cmdlnet that is used to modify the properties of a share.

For example to modify the description of a network share , we use -Description :

Set-SmbShare share -Description “Kikooo”

Enable SMB encryption on the movie share:

Set-SmbShare –Name <sharename> -EncryptData $true

Replace $ true with $ false to disable it

Then to disable the caching on the movie share:

Set-SmbShare film -CachingMode none -Force

DELETE A NETWORK SHARE

Powershell makes it easy to delete a folder share with Remove-SmbShare .

For example to delete the network share with the name Data:

Remove-SmbShare -Name “Data”

CREATE A NETWORK DRIVE OR MAP A NETWORK DRIVE

To create a network drive in PowerShell, we use New-SmbMapping .

For example to map a network drive X to the \\ PC \ MyShare

New-SmbMapping -LocalPath ‘X:’ -RemotePath ‘\\PC\MonPartage’

 

by Abdullah Sam
I’m a teacher, researcher and writer. I write about study subjects to improve the learning of college and university students. I write top Quality study notes Mostly, Tech, Games, Education, And Solutions/Tips and Tricks. I am a person who helps students to acquire knowledge, competence or virtue.

Leave a Comment