用 PowerShell 的 New-SmbShare 共享文件夹

用 SmbShare 模块创建局域网 SMB 共享并精确设置访问权限,相当于脚本版的"共享"选项卡。

Windows文件共享管理员PowerShell中风险
在管理员 PowerShell 中运行 New-SmbShare -Name 'Reports' -Path 'C:\Reports' -FullAccess 'Everyone'。

01命令

命令
$ New-SmbShare -Name 'Reports' -Path 'C:\Reports' -FullAccess 'Everyone' -ReadAccess 'Guest'

02作用说明

New-SmbShare 做的事和右键"属性→共享"一样,但可写进脚本复用:共享名就是其他电脑访问时输入的 \\你的电脑\Reports;访问参数(-FullAccess、-ReadAccess、-ChangeAccess、-NoAccess)控制谁能读、谁能写。注意 NTFS 权限仍然独立生效,用户需要在两层都有权限才能真正访问。这是 PowerShell 5.1+ 的做法;老式写法是 net share Reports=C:\Reports /GRANT:Everyone,FULL。

03风险

风险中风险
使用条件Run in an elevated (admin) PowerShell; Server service must be running; folder path must exist.
!注意: 对 Everyone 开放共享意味着整个局域网都能访问,建议只授权具体账户;且 NTFS 权限也必须允许访问。

04撤销

撤销
$ Remove-SmbShare -Name 'Reports' -Force

05来源

  • New-SmbShare
    Microsoft Learn · vendor-doc · 2026-09-17
    依据: New-SmbShare creates an SMB share with -Name, -Path and access lists such as -FullAccess and -ReadAccess.
  • New-SmbShare - SS64
    SS64 · technical · 2026-09-17
    依据: Documents share creation parameters and permission groups for the SmbShare module.

最后验证: 2026-09-18 · 分类: 文件与文件夹

06相关方案