Skip to main content

How to Share a Printer on the Network in Batch Script

In a small office or home lab environment, you don't always have a dedicated print server. Often, you need to designate a single workstation as the "Host" for a printer so that other computers on the same Wi-Fi or Ethernet can use it. Manually right-clicking a printer, going to the "Sharing" tab, and typing a share name is tedious for rapid deployments. A Batch script can use the WMIC command or PowerShell's Set-Printer to instantly flag a local printer as "Shared," making it visible and available to every authorized user on your network.

This guide will explain how to programmatically share printers.

Method: Enabling the Share (PowerShell)

PowerShell's Set-Printer cmdlet provides the most reliable way to modify sharing properties and is the recommended approach for modern Windows systems.

@echo off
set "PrinterName=HP_LaserJet_Local"
set "ShareName=Office_Public_HP"

echo [ACTION] Sharing %PrinterName% as \\%COMPUTERNAME%\%ShareName%...

:: Verify the printer exists
powershell -NoProfile -Command ^
"$p = Get-Printer -Name '%PrinterName%' -ErrorAction SilentlyContinue;" ^
"if (-not $p) { Write-Host '[ERROR] Printer not found: %PrinterName%'; exit 1 };" ^
"if ($p.Shared -and $p.ShareName -eq '%ShareName%') {" ^
" Write-Host '[INFO] Printer is already shared as %ShareName%.'; exit 0 };" ^
"try {" ^
" Set-Printer -Name '%PrinterName%' -Shared $true -ShareName '%ShareName%';" ^
" Write-Host '[SUCCESS] Printer is now visible on the network as \\%COMPUTERNAME%\%ShareName%'" ^
"} catch { Write-Host '[ERROR]' $_.Exception.Message; exit 1 }"

if %errorlevel% neq 0 (
echo [ERROR] Failed to share. Ensure you are running as ADMIN.
)

pause
warning

Administrative Rights. Modifying network sharing properties for hardware is a security-sensitive action. You MUST run your script as an Administrator.

Method 2: Sharing with Firewall Configuration

Sharing a printer is only half the process, other computers also need to reach it through the firewall. This method combines the sharing command with the necessary firewall exception.

@echo off
setlocal

set "PrinterName=Zebra_Labeler"
set "ShareName=Warehouse_Zebra"

echo [ACTION] Enabling network sharing for %PrinterName%...

powershell -NoProfile -Command ^
"try {" ^
" Set-Printer -Name '%PrinterName%' -Shared $true -ShareName '%ShareName%' -ErrorAction Stop;" ^
" Write-Host '[SUCCESS] Printer shared as \\%COMPUTERNAME%\%ShareName%';" ^
"} catch {" ^
" Write-Host '[ERROR] Printer not found or failed to share:' $_.Exception.Message;" ^
" exit 1;" ^
"}"

if %errorlevel% neq 0 (
echo [ERROR] Sharing failed. Ensure the printer exists and you are running as ADMIN.
pause
exit /b 1
)

echo [ACTION] Configuring firewall for File and Printer Sharing...
netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes >nul 2>&1

if %errorlevel% equ 0 (
echo [SUCCESS] Firewall rules updated. Other computers can now connect.
) else (
echo [WARNING] Firewall update failed. Other computers may not see this printer.
)

echo.
pause

Method 3: Unsharing a Printer

When a project ends or you need to hide a printer for maintenance, you can instantly revoke its network access.

@echo off
set "PrinterName=Confidential_Laser"

echo [SECURITY] Disabling network sharing for %PrinterName%...

:: Verify the printer exists and is currently shared
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 '[DONE] Printer is now local only. Network access revoked.'" ^
"} catch { Write-Host '[ERROR]' $_.Exception.Message; exit 1 }"

if %errorlevel% neq 0 (
echo [ERROR] Failed to unshare. Ensure you are running as ADMIN.
)

pause

How to Avoid Common Errors

Wrong Way: Sharing without a name

If you set a printer as shared but don't provide a ShareName, the printer will technically be shared, but other computers might see a generic "USB001" or no name at all, making it impossible to connect.

Correct Way: Always explicitly set the ShareName (Method 1 or 2). This ensures the network path (e.g., \\PC-NAME\MyPrinter) is predictable and easy to remember.

Problem: Firewall Blocks

Even if your script says "Success," other computers might not find the printer if the Windows Firewall is blocking "File and Printer Sharing."

Solution: Include the firewall configuration in your sharing script (as shown in Method 2) to ensure the necessary ports are open: netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes

Best Practices and Rules

1. Simple Share Names

Avoid using spaces or special characters in your network share names. Use Main_Printer instead of The Main Printer (Floor 1). This makes it much easier for other users to map the printer via script.

2. Identify "Computer Name"

The full path of your shared printer will be \\%COMPUTERNAME%\%ShareName%. Ensure your computer has a meaningful name (like RECEPTION-PC) before you start sharing hardware to the network.

3. Permissions

Sharing a printer doesn't mean "Everyone can print." You must still ensure that the network users have appropriate permissions in the "Security" tab. By default, Windows allows everyone on the local network to print, but always verify.

Conclusions

Sharing a printer via Batch script is a powerful way to turn any workstation into a functional print server. By moving from manual GUI configuration to automated sharing commands, you gain the ability to scale your office infrastructure instantly and with 100% consistency. This professional level of control is essential for anyone managing small office networks or lab environments where hardware resources must be shared efficiently.