How to Remove a Network Share in a Batch Script
Removing (or "unsharing") a network share is a common administrative task needed to decommission a resource, restrict access to a folder, or clean up an old server configuration. While this can be done through the GUI, automating the process with a batch script is essential for server cleanup scripts and configuration management. The standard, built-in command for managing all network shares is NET SHARE.
This guide will teach you how to use the NET SHARE command with its /DELETE switch to permanently remove a network share from a local computer. You will learn the correct syntax and the critical importance of running the script with administrator privileges.
CRITICAL WARNING: Removing a network share makes the folder inaccessible to network users. This is a high-privilege operation that affects the system's configuration. This script must be run with full administrator privileges.
The Core Command: NET SHARE
The NET SHARE command is the command-line interface for managing shared folders on a Windows machine. It can be used to create, view, and, most importantly for this guide, delete network shares.
Syntax for Deletion: NET SHARE ShareName /DELETE
ShareName: The name of the share as it appears on the network (e.g.,PublicData), not the local folder path./DELETE: The switch that instructs the command to remove the share. This can be shortened to/D.
Basic Example: Deleting a Share
This script removes a share named OldData from the local computer.
@ECHO OFF
REM This script MUST be run as an Administrator.
SET "ShareToRemove=OldData"
ECHO --- Removing Network Share ---
ECHO WARNING: This will unshare the folder. Network users will lose access.
ECHO Target share: %ShareToRemove%
ECHO.
PAUSE
ECHO.
ECHO Deleting the '%ShareToRemove%' share...
NET SHARE %ShareToRemove% /DELETE
IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] The share was deleted successfully.
) ELSE (
ECHO [FAILURE] An error occurred. Errorlevel: %ERRORLEVEL%
)
The Essential Prerequisite: Listing Shares First
Before you delete a share, you must know its exact share name. This is not the same as the folder path. The best way to get a list of all current shares is to run NET SHARE with no arguments.
Command: NET SHARE
This command lists the share name, the local resource path it points to, and any remarks.
Share name Resource Remark
-------------------------------------------------------------------------------
C$ C:\ Default share
IPC$ Remote IPC
ADMIN$ C:\WINDOWS Remote Admin
Public C:\Users\Public Public User Data
OldData E:\Legacy\Data Old Project Files to be removed
From this list, you can confirm that the name of the share you want to delete is indeed OldData.
Key NET SHARE Parameters Explained
ShareName: The name of the share to act upon./DELETEor/D: (Required) The switch to delete the specified share.
The NET SHARE command has other parameters (/GRANT, /USERS, etc.), but these are only used for creating shares, not deleting them.
Common Pitfalls and How to Solve Them
Problem: "System error 5 has occurred. Access is denied."
This is the most common error and it means your script does not have sufficient privileges to modify the system's share configuration.
Solution: You must run the script as an Administrator. Right-click your .bat file or cmd.exe and select "Run as administrator." There is no alternative for this operation.
This Does Not Delete the Folder!
A critical point to understand is that NET SHARE ... /DELETE only removes the network share itself. It does not delete the underlying folder or its data. The files and folders in E:\Legacy\Data from our example will remain on the disk, they just won't be accessible from the network via the OldData share anymore.
Solution: If your goal is to both unshare and delete the data, this must be a two-step process in your script.
@ECHO OFF
REM Run as Administrator.
SET "ShareName=OldData"
SET "FolderPath=E:\Legacy\Data"
ECHO Step 1: Removing the network share...
NET SHARE %ShareName% /DELETE
ECHO Step 2: Deleting the local folder and its contents...
RD /S /Q "%FolderPath%"
ECHO Deletion and unsharing complete.
Practical Example: A Server Decommissioning Script
This script automates part of a server decommissioning process. It iterates through a list of known user-created shares and deletes them, leaving only the default administrative shares.
@ECHO OFF
SETLOCAL
REM This script must be run as an Administrator.
ECHO --- Server Share Cleanup Script ---
ECHO This will remove all non-administrative shares from this server.
ECHO.
PAUSE
ECHO.
REM Use WMIC for a clean, parsable list of shares.
REM We filter out shares with a '$' in the name to protect admin shares.
FOR /F "skip=1 tokens=1" %%S IN ('WMIC SHARE GET Name ^| FINDSTR /V "$" ') DO (
ECHO Deleting share: "%%S"...
NET SHARE "%%S" /DELETE
)
ECHO.
ECHO --- Cleanup complete. Current shares: ---
NET SHARE
ENDLOCAL
Conclusion
The NET SHARE command is the definitive tool for removing network shares from a batch script. The process is simple but requires care.
For a successful and safe operation:
- Always run your script as an Administrator.
- Use
NET SHAREwith no arguments first to get the exact name of the share you want to delete. - Use the syntax
NET SHARE ShareName /DELETEto remove the share. - Remember that this action only removes the share, not the data. If you also want to delete the data, you must add a separate
RD /S /Qcommand.