How to remove windows folder sharing using PowerShell?


To remove the windows folder sharing using PowerShell, we can use the Remove-Smbshare command. For example,

PS C:\Temp> Remove-SmbShare -Name DSC
Confirm
Are you sure you want to perform this action?
Performing operation 'Remove-Share' on Target '*,DSC'.
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):

Once you use this command, it will ask for confirmation. To remove the sharing forcefully, use - Force parameter. For example,

Remove-SmbShare -Name DSC -Force

To remove the shared folder permissions on the remote computer, you need to use the CIM session.

In the below example, the DSC folder will be removed from sharing from the LabMachine2k16 machine.

$sess = New-CimSession -ComputerName LabMachine2k16
Remove-SmbShare -Name DSC -Session $sess -Force

Updated on: 01-Mar-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements