Remove SmbShare - PowerShell
Overview
Remove-SmbShare removes a file share from a Windows Server. This is useful for cleaning up unused shares or for removing access to data from unauthorized users.
Syntax
Remove-SmbShare [-Name] <String[]> [-Path] <String> [-Force] [-ComputerName <String>] [-Credential <PSCredential>] [-WhatIf] [-Confirm]
Options/Flags
- -Name: Specifies the name of the share to remove.
- -Path: Specifies the path to the share to remove.
- -Force: Forces the removal of the share, even if it is in use.
- -ComputerName: Specifies the name of the computer that hosts the share.
- -Credential: Specifies the credentials to use to connect to the computer that hosts the share.
- -WhatIf: Displays what the command would do without actually executing it.
- -Confirm: Prompts you to confirm the operation before executing it.
Examples
Removing a share by name
Remove-SmbShare -Name "MyShare"
Removing a share by path
Remove-SmbShare -Path "\\computername\sharename"
Removing a share forcibly
Remove-SmbShare -Name "MyShare" -Force
Common Issues
One common issue that you may encounter is that the share is still in use. If you try to remove a share that is still in use, you will receive an error message. You can use the -Force parameter to force the removal of the share, even if it is in use. However, this is not recommended as it can cause data loss.
Integration
Remove-SmbShare can be combined with other PowerShell commands to automate the removal of shares. For example, you could use the following command to remove all shares on a computer:
Get-SmbShare -ComputerName "computername" | Remove-SmbShare -Force
Related Commands
- New-SmbShare: Creates a new file share.
- Get-SmbShare: Gets a file share.
- Set-SmbShare: Sets the properties of a file share.