Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / PowerShell / How to Manage Windows File Shares Using PowerShell

March 12, 2024 PowerShellWindows 10Windows Server 2019

How to Manage Windows File Shares Using PowerShell

The built-in SMBShare PowerShell module allows you to create, configure, and manage shared network folders in Windows. In this article, we will look at how to manage file shares (SMB network folders) using PowerShell. You may use these examples to quickly and easily manage your SMB file servers and shared folders in different automation scenarios.

Contents:
  • Creating a Shared Folder onWindows with PowerShell
  • How to View and Manage Open Files in Windows Shares?
  • Map SMB Network Drives with SmbMapping Cmdlets

The SMBShare module contains 42 PowerShell cmdlets to manage shared network folders. You can display the full list of cmdlets in the module:

Get-Command -Module SMBShare

SMBShare PowerShell module allows to manage shared folder on Windows

To display the current configuration of your Windows SMB server:

Get-SmbServerConfiguration

AnnounceComment :
AnnounceServer : False
AsynchronousCredits : 64
AuditSmb1Access : False
AutoDisconnectTimeout : 15
AutoShareServer : True
AutoShareWorkstation : True
CachedOpenLimit : 10
DurableHandleV2TimeoutInSeconds : 180
EnableAuthenticateUserSharing : False
EnableDownlevelTimewarp : False
EnableForcedLogoff : True
EnableLeasing : True
EnableMultiChannel : True
EnableOplocks : True
EnableSecuritySignature : False
EnableSMB1Protocol : True
EnableSMB2Protocol : True
EnableStrictNameChecking : True
EncryptData : False
IrpStackSize : 15
KeepAliveTime : 2
MaxChannelPerSession : 32
MaxMpxCount : 50
MaxSessionPerConnection : 16384
MaxThreadsPerQueue : 20
MaxWorkItems : 1
NullSessionPipes :
NullSessionShares :
OplockBreakWait : 35
PendingClientTimeoutInSeconds : 120
RejectUnencryptedAccess : True
RequireSecuritySignature : False
ServerHidden : True
Smb2CreditsMax : 2048
Smb2CreditsMin : 128
SmbServerNameHardeningLevel : 0
TreatHostAsStableStorage : False
ValidateAliasNotCircular : True
ValidateShareScope : True
ValidateShareScopeNotAliased : True
ValidateTargetName : True

You can change SMB server options using the Set-SmbServerConfiguration cmdlet.

For example, to disable the legacy SMB 1 protocol, run the command below:

Set-SmbServerConfiguration -EnableSMB1Protocol $false -Force

To display a list of SMB protocol versions used by active clients to connect to file shares on the current SMB file server:

Get-SmbConnection

To set bandwidth limits for SMB file traffic, you may configure the QoS policy for your SMB server (How to configure SMB bandwidth limits?). For example, the command below will limit the maximum bandwidth for SMB traffic to 10 MB:

Set-SmbBandwidthLimit -Category Default -BytesPerSecond 10MB

Creating a Shared Folder onWindows with PowerShell

To display a list of shared folders available on a computer, run this command:

Get-SmbShare

Get-SmbShare - list shared folders on Windows

You can see several administrative shares and the Distr shared folder on this computer.

To create a new shared folder, run the command below:

New-SmbShare -Name Scripts -Path C:\PS -FullAccess woshub\mun_admins, woshub\mun-man01$ -ChangeAccess "woshub\mun-man01_scripts_rw" -ReadAccess "$env:USERDOMAIN\domain users" –description "PowerShell scripts for admin"

In this example, we created a shared folder and granted access to domain groups and one computer account.

Additionally, when creating a shared folder, you can use the following options:

  • -CachingMode [None|Manual|Programs|Documents|BranchCache] –set a caching mode for offline access (Windows offline files);
  • -EncryptData $True – to enable SMB traffic encryption;
  • -FolderEnumerationMode [AccessBased | Unrestricted] – to enable Access-based Enumeration. Allows to hide objects a user doesn’t have permission to access from the shared folder;
  • -CompressData $True – to enable SMB compression for file transfers;
  • -ConcurrentUserLimit 50 – to set a limit of simultaneous connections to the folder (0 by default, unlimited);
  • -Temporary – to create a temporary shared folder (disappears after the next Windows restart).

You can display a full list of shared folder settings:

Get-SmbShare -Name scripts| select *

Get-SmbShare settings witn powershell

To remove a shared network folder:

Remove-SmbShare Scripts

To add write permission for a user to the list ACL of the shared folder:

Grant-SmbShareAccess -Name Scripts -AccountName "woshub\b.hoffmann" -AccessRight Change –force

Display the current shared folder access list:

Get-SmbShareAccess scripts

get-smbshareaccess permissions

To remove a security group from a share’s ACL:

Revoke-SmbShareAccess -Name Scripts -AccountName Everyone –Force

To force block access to a shared folder (a deny permission has a higher priority):

Block-SmbShareAccess -Name Scripts -AccountName woshub\ExternalGuests -Force

In most cases, you should use the Everyone -> RW permissions on a shared folder. In this case, the folder permissions are determined at the NTFS level.

You can get the current NTFS ACL for a shared folder using this command:

(get-acl \\mun-man01\scripts).access

To change NTFS permissions, use the Set-Acl cmdlet.

How to View and Manage Open Files in Windows Shares?

You can use SMBShare cmdlets to view a list of files opened by users on a shared folder on a Windows file server.

To display a list of opened files with usernames, computer names (IP addresses), and file paths:

Get-SmbOpenFile|select ClientUserName,ClientComputerName,Path,SessionID

To show a list of files opened by a specific user:

Get-SMBOpenFile –ClientUserName "woshub\b.hoffmann" |select ClientComputerName,Path

To close a file a user opened and locked by a remote user:

$sessn = New-CIMSession –Computername munfs01
Get-SMBOpenFile -CIMSession $sessn | where {$_.Path –like "*sale_report2022.docx"} | Close-SMBOpenFile -CIMSession $sessn

Learn more about how to manage open files in SMB shares on Windows.

Map SMB Network Drives with SmbMapping Cmdlets

SmbMapping cmdlets are used to manage network drives.

To map a network shared folder to the network drive U:, run the command below:

New-SmbMapping -LocalPath U: -RemotePath \\munfs01\scripts -UserName b.hoffmann -Password my22pass –Persistent $true -SaveCredential

  • Without the Persistent option, the mapped network drive will only be available until the computer is restarted;
  • The SaveCredential option allows saving user credentials to the Windows Credential Manager.

To display a list of mapped network folders:

Get-SmbMapping

To remove a network drive:

Remove-SmbMapping U: -force

You can use GPO to map network drives in Windows.
0 comment
3
Facebook Twitter Google + Pinterest
previous post
Outlook: Your Server Does Not Support the Connection Encryption Type
next post
Setup a Remote Desktop Gateway on Windows Server

Related Reading

View Windows Update History with PowerShell (CMD)

April 30, 2025

Change BIOS from Legacy to UEFI without Reinstalling...

April 21, 2025

Uninstalling Windows Updates via CMD/PowerShell

April 18, 2025

Allowing Ping (ICMP Echo) Responses in Windows Firewall

April 15, 2025

How to Pause (Delay) Update Installation on Windows...

April 11, 2025

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMware
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025
  • How to Write Logs to the Windows Event Viewer from PowerShell/CMD

    March 3, 2025
  • How to Hide (Block) a Specific Windows Update

    February 25, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Install and Manage Windows Updates with PowerShell (PSWindowsUpdate)
  • How to Download Offline Installer (APPX/MSIX) for Microsoft Store App
  • Fix: Remote Desktop Licensing Mode is not Configured
  • How to Delete Old User Profiles in Windows
  • Configuring Port Forwarding in Windows
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top