Modules/Dismount-Volume.psm1
<#
.Synopsis Dismounts a currently mounted volume. .Description Dismounts a currently mounted volume. This function is intended to be part of a modernized replacement for mountvol, specifically the mountvol / and mountvol /d syntax. .Parameter DeviceID The unique volume name that is the target of the mount point. The volume name uses the following syntax, where GUID is a globally unique identifier: \\\\?\Volume\{GUID}\ The {} brackets are required. .Parameter DriveLetter The drive letter at which the volume is mounted. .Parameter Label The name or label of the mounted volume. .Parameter ShowDetailedResults Outputs the raw results of the .Dismount method to the console for review by the user. For information regarding the codes generated by this method, please see Microsoft's Win32_Volume .Dismount method documentation: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/vdswmi/dismount-method-in-class-win32-volume .Example # Dismount a volume by drive letter. Dismount-Volume -DriveLetter E: .Example # Dismount a volume by drive letter and output detailed results. Dismount-Volume -DriveLetter E: -ShowDetailedResults SAMPLE OUTPUT: __GENUS : 2 __CLASS : __PARAMETERS __SUPERCLASS : __DYNASTY : __PARAMETERS __RELPATH : __PROPERTY_COUNT : 1 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : ReturnValue : 0 PSComputerName : #> Function Dismount-Volume { [CmdletBinding()] Param( [Parameter( ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $True)] [object]$DeviceID, [Parameter( ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $True)] [object]$DriveLetter, [Parameter( ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $True)] [object]$Label, [Parameter()] [switch]$ShowDetailedResults ) begin { if ($PSBoundParameters.ContainsKey('DeviceID')) { $Drive = Get-WMIObject -Class Win32_Volume | Where-Object {$_.DeviceID -eq $DeviceID} } elseif ($PSBoundParameters.ContainsKey('DriveLetter')) { $Drive = Get-WMIObject -Class Win32_Volume | Where-Object {$_.DriveLetter -like $DriveLetter} } elseif ($PSBoundParameters.ContainsKey('Label')) { $Drive = Get-WMIObject -Class Win32_Volume | Where-Object {$_.Label -like $Label} } else { Write-Error "Please define a drive with either DeviceID, DriveLetter, or Label." } } process { $Drive.DriveLetter = $null $PutResult = $Drive.put() $DismountResults = $Drive.dismount($false, $true) } end { if ($PSBoundParameters.ContainsKey('ShowDetailedResults')) { $DismountResults } $DriveOutput = [PSCustomObject]@{ DeviceID = $Drive.DeviceID Status = $null } if ($DismountResults.ReturnValue -eq 0) { $DriveOutput.status = "Dismounted" } else {$DriveOutput.status = "Dismount Failed"} Write-Output $DriveOutPut | FT } } |