Public/Remove-specDriveMap.ps1

Function Remove-specDriveMap {
    <#
    .SYNOPSIS
        Removes mapped drives using Remove-SmbMapping.
 
    .DESCRIPTION
        The Remove-specDriveMap function removes all mapped drives or specific mapped drives.
 
    .PARAMETER DriveLetter
        Specifies the drive letter(s) for which to remove mapping.
 
        Type the drive letter(s) as a string or provide them via the pipeline.
 
    .EXAMPLE
        Remove-specDriveMap -DriveLetter 'N:'
 
        Removes the mapped drive with the letter 'N:'.
 
    .EXAMPLE
        'N:' | Remove-specDriveMap
 
        Removes the mapped drive with the letter 'N:' using pipeline input.
 
    .EXAMPLE
        'N:','O:' | Remove-specDriveMap
 
        Removes the mapped drives with the letters 'N:' and 'O:' using pipeline input.
 
    .NOTES
        Author: owen.heaume
        Version: 1.0
    #>


    [cmdletbinding(SupportsShouldProcess = $true)]
    param (
        [Parameter(
            Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            Position = 0)]
        [ValidateLength(1, 2)]
        [alias ('Drive')]
        [alias('RowKey')]
        [string[]]$DriveLetter
    )

    Begin {
        $driveMap = Get-SmbMapping
    }

    Process {
        if ($DriveLetter) {
            foreach ($letter in $DriveLetter) {
                # if the drive letter does not end in a colon then add it
                if ($letter[-1] -ne ':') {
                    $letter = $letter + ':'
                }

                $map = $driveMap | Where-Object -Property 'LocalPath' -eq $letter

                if ($map) {
                    if ($PSCmdlet.ShouldProcess($letter, 'Remove-SmbMapping')) {
                        write-host "Removing drive $($letter)" -ForegroundColor DarkCyan

                        Remove-SmbMapping -LocalPath $letter -Force -UpdateProfile
                        Write-Host "OK" -ForegroundColor DarkGreen
                    }
                } else {
                    Write-Warning -Message "No drive map found for $letter"
                }
            }
        } else {
            # if no drivemaps were found write a warning
            if ($driveMap.count -eq 0) {
                Write-Warning -Message "No drive maps found"
            } else {
                $drivemap | % {
                    if ($PSCmdlet.ShouldProcess($_.LocalPath, 'Remove-SmbMapping')) {
                        write-host "Removing drive $($_.LocalPath)" -ForegroundColor DarkCyan
                        Remove-SmbMapping -LocalPath $_.LocalPath -Force -UpdateProfile
                        Write-Host "OK" -ForegroundColor DarkGreen
                    }
                }
            }
        }
    }
}