Public/Mount-FsLogixVHD.ps1
<#
.SYNOPSIS Mounts an FsLogix VHD file and assigns a specified drive letter. .DESCRIPTION The Mount-FsLogixVHD function mounts a specified VHDX file if it is not already in use and assigns it a new drive letter. .PARAMETER VhdxPath The path to the VHDX file that needs to be mounted. .PARAMETER DriveLetter The drive letter to assign to the mounted VHDX. Default is 'E'. .EXAMPLE Mount-FsLogixVHD -VhdxPath "C:\VHDs\example.vhdx" -DriveLetter 'F' Attempts to mount the VHDX located at "C:\VHDs\example.vhdx" and assigns it the drive letter 'F'. .NOTES Requires administrative privileges to run. #> Function Mount-FsLogixVHD { [CmdletBinding(SupportsShouldProcess = $True)] Param ( [Parameter(Mandatory = $True)] [string]$VhdxPath, [Parameter(Mandatory = $False)] [string]$DriveLetter = 'E' ) Begin { $metadataPath = $VhdxPath + ".metadata" if (Test-Path -Path $metadataPath) { Write-Warning "The VHDX file is currently in use according to its metadata." return } } Process { if ($PSCmdlet.ShouldProcess($VhdxPath, "Mount VHDX")) { $vhdx = Mount-VHD -Path $VhdxPath -Passthru $disk = Get-Disk -Number ($vhdx | Select-Object -ExpandProperty Number) $partitions = Get-Partition -DiskNumber $disk.Number foreach ($partition in $partitions) { if ($null -ne $partition.DriveLetter) { $partition | Set-Partition -NewDriveLetter $DriveLetter -ErrorAction SilentlyContinue } } } } } |