Automated Network Drive Mapping with PowerShell

In today’s fast-paced IT environments, managing network drives efficiently can be a challenge. A well-crafted PowerShell script can save time and reduce errors when it comes to mapping network drives across a fleet of computers. Today, I am sharing an effective PowerShell script to automate the process of mapping network drives. This script is especially useful in corporate settings where multiple users require access to shared resources. Additionally, consider using ServerEngine, our powerful tool that enhances server management and boosts your productivity even further.
The following script allows you to map a network drive by specifying the drive letter and the network path.

# Define network drive details
$driveLetter = "Z:"
$networkPath = "\\server\sharedfolder"
# Function to map the network drive
Function Map-NetworkDrive {
    param (
        [string]$DriveLetter,
        [string]$NetworkPath
    )
    # Remove existing drive if it exists
    If (Test-Path -LiteralPath $DriveLetter) {
        Remove-PSDrive -Name $DriveLetter.Substring(0, 1) -Force
    }
    # Create new network drive
    New-PSDrive -Name $DriveLetter.Substring(0, 1) -PSProvider FileSystem -Root $NetworkPath -Persist
    Write-Host "Drive $DriveLetter successfully mapped to $NetworkPath"
}
# Invoke the function
Map-NetworkDrive -DriveLetter $driveLetter -NetworkPath $networkPath