How to Unshare a Printer on Network in Batch Script
Security and resource management are just as much about "Closing Doors" as they are about opening them. If a temporary project has ended, or if a printer contains sensitive data that should no longer be accessible to everyone on the Wi-Fi, you need a way to instantly revoke its network status. While right-clicking in the GUI works for one machine, a Batch script allows you to "Decommission" printer shares across multiple workstations instantly. By using WMIC or PowerShell to set the Shared attribute to false, you ensure the printer returns to being a "Local-Only" device.
This guide will explain how to remove printer shares using Batch.
Method: Revoking the Share (PowerShell)
PowerShell's Set-Printer cmdlet is the most reliable way to toggle the shared status of a printer on modern Windows systems.
Implementation Script
@echo off
set "PrinterName=Shared_MFP_Lobby"
echo [ACTION] Revoking network share for: %PrinterName%...
:: Verify the printer exists and check its current share status
powershell -NoProfile -Command ^
"$p = Get-Printer -Name '%PrinterName%' -ErrorAction SilentlyContinue;" ^
"if (-not $p) { Write-Host '[ERROR] Printer not found: %PrinterName%'; exit 1 };" ^
"if (-not $p.Shared) { Write-Host '[INFO] Printer is not currently shared.'; exit 0 };" ^
"try {" ^
" Set-Printer -Name '%PrinterName%' -Shared $false;" ^
" Write-Host '[SUCCESS] The printer is no longer visible to other network users.';" ^
" Write-Host 'Previous share name was:' $p.ShareName" ^
"} catch { Write-Host '[ERROR]' $_.Exception.Message; exit 1 }"
if %errorlevel% neq 0 (
echo [ERROR] Failed to unshare. Ensure you are running as ADMIN.
)
echo %date% %time% - Revoked share for %PrinterName% >> security_audit.log
pause
Administrative Rights. Stopping a network service (like a printer share) requires high-level system permissions. You MUST run your script as an Administrator.
Method 2: Unshare with Active Job Check
Before revoking a share, it is good practice to verify no one is actively printing to avoid interrupting a legitimate business process.
@echo off
set "PrinterName=Zebra_Direct_USB"
echo [SECURITY] Preparing to disable external access to %PrinterName%...
:: Check for active print jobs before unsharing
powershell -NoProfile -Command ^
"$p = Get-Printer -Name '%PrinterName%' -ErrorAction SilentlyContinue;" ^
"if (-not $p) { Write-Host '[ERROR] Printer not found: %PrinterName%'; exit 1 };" ^
"if (-not $p.Shared) { Write-Host '[INFO] Printer is not currently shared.'; exit 0 };" ^
"$jobs = Get-PrintJob -PrinterName '%PrinterName%' -ErrorAction SilentlyContinue;" ^
"if ($jobs) {" ^
" Write-Host '[WARNING]' @($jobs).Count 'active job(s) in queue.';" ^
" Write-Host 'Unsharing now may interrupt these jobs.'" ^
"}" ^
"else { Write-Host '[INFO] No active jobs in queue. Safe to proceed.' }"
echo.
set /p "confirm=Proceed with unsharing? (Y/N): "
if /i "%confirm%" neq "Y" (
echo [EXIT] Operation cancelled by user.
pause
exit /b 0
)
powershell -NoProfile -Command ^
"try {" ^
" Set-Printer -Name '%PrinterName%' -Shared $false;" ^
" Write-Host '[SUCCESS] Share terminated for %PrinterName%.'" ^
"} catch { Write-Host '[ERROR]' $_.Exception.Message; exit 1 }"
if %errorlevel% neq 0 (
echo [ERROR] Failed to unshare. Ensure you are running as ADMIN.
)
pause
Method 3: The "Deep Clean" (Unshare All Printers)
Use this script if you are moving a workstation to a different department and want to ensure NO printers are accidentally left shared with the old team.
@echo off
echo [CLEANUP] Revoking all active printer shares from this workstation...
echo.
powershell -NoProfile -Command ^
"$shared = Get-Printer | Where-Object { $_.Shared -eq $true };" ^
"if (-not $shared) { Write-Host '[INFO] No shared printers found.'; exit 0 };" ^
"foreach ($p in $shared) {" ^
" try {" ^
" Set-Printer -Name $p.Name -Shared $false;" ^
" Write-Host '[UNSHARED]' $p.Name '(was:' $p.ShareName ')'" ^
" } catch {" ^
" Write-Host '[FAILED]' $p.Name ':' $_.Exception.Message" ^
" }" ^
"};" ^
"Write-Host '';" ^
"Write-Host 'Processed' @($shared).Count 'printer(s).'"
echo.
echo %date% %time% - All printer shares revoked >> security_audit.log
pause
How to Avoid Common Errors
Wrong Way: Thinking "Unsharing" deletes the printer
When you unshare a printer, the hardware stays on your computer. You can still print to it locally. All you are doing is deleting the "Network Path" (e.g., \\Your-PC\Printer) that other people were using.
Correct Way: Use Method 1 to revoke the share. This is a non-destructive security action that only affects network users, not the local operator.
Problem: Active Connections
If another user is in the middle of printing a large document when you run the "Unshare" script, their document might stall or fail.
Solution: Use Method 2 to check for active jobs before unsharing to ensure you aren't interrupting a legitimate business process.
Best Practices and Rules
1. Identify "Orphaned" Shares
Check your list of shared printers before you start. Sometimes Windows "remembers" a share even if the printer is unplugged.
powershell -NoProfile -Command "Get-Printer | Where-Object { $_.Shared } | Format-Table Name, ShareName"
2. Firewall Cleanup
Once you stop sharing printers, you may want to close the corresponding holes in your firewall for maximum security:
netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=No
3. Log the Shutdown
In high-security environments, log whenever a device is taken off the network. This provides proof of decommissioning for your security audits.
echo %date% %time% - Revoked Share for %PrinterName% >> security_audit.log
Conclusions
Unsharing a printer via Batch script is a vital task for any system administrator focused on security and clean resource management. By moving from manual per-machine configuration to automated revocation commands, you gain the ability to harden your network environment instantly. This professional level of control ensures that your shared resources are only accessible when and where they are needed, maintaining a high standard of privacy and efficiency in your organization.