Public/New-UserProfileDisk.ps1

<#
.SYNOPSIS
    Creates and manages a VHD for a user profile.
 
.DESCRIPTION
    This function creates a new VHD for a user profile, formats it, assigns a drive letter, and manages the permissions for the specified user. It can also mount an existing VHD.
 
.PARAMETER Target
    The target path for the VHD.
 
.PARAMETER Username
    The username for which the VHD will be created or managed.
 
.PARAMETER Size
    The size of the VHD in GB.
 
.PARAMETER SectorSize
    The sector size for the VHD (optional). Valid values are '4K' or '512'.
 
.PARAMETER PrimarySearchRoot
    The primary Active Directory search root.
 
.PARAMETER SecondarySearchRoot
    The secondary Active Directory search root.
 
.PARAMETER LogPath
    The path to the log file where messages will be recorded.
 
.EXAMPLE
    New-UserProfileDisk -Target "C:\VHDs\UserProfile.vhdx" -Username "jdoe" -Size 50 -SectorSize '4K' -LogPath "C:\Logs\ProfileDisk.log"
 
    # Creates a new User Profile Disk
#>


function New-UserProfileDisk {
    [CmdletBinding(SupportsShouldProcess = $True)]
    Param (
        [Parameter(Mandatory = $True)]
        [string]$Target,

        [Parameter(Mandatory = $True)]
        [string]$Username,

        [Parameter(Mandatory = $True)]
        [uint64]$Size,

        [Parameter(Mandatory = $False)]
        [ValidateSet('4K', '512')]
        [string]$SectorSize,

        [Parameter(Mandatory = $False)]
        [string]$LogPath
    )

    $OutputObject = @()
    if ($SectorSize -eq '4K') {
        $SectorSizeBytes = 4096
    }
    if ($SectorSize -eq '512') {
        $SectorSizeBytes = 512
    }

    if ($pscmdlet.ShouldProcess($Name, 'Action')) {
        if ($Target -ne "Cannot Copy") {
            if (!(Test-Path ($Target.Substring(0, $Target.LastIndexOf('.')) + "*"))) {

                New-FSLogixVHD -Target $Target -Size $Size -SectorSizeBytes $SectorSizeBytes
                icacls "$Target" /grant $Username`:f /t
                Write-Output "Mounting VHD $Target"
                Write-Log -Message "Mounting VHD $Target" -LogPath $LogPath
                Mount-VHD $Target -ErrorAction SilentlyContinue
                $drive = (Get-DiskImage -ImagePath $Target | Get-Disk | Get-Partition).DriveLetter
                Start-Sleep 6
                if (($Drive | Measure-Object).count -gt 1) {
                    $Drive = $drive[1]
                }
                $Item = New-Object system.object
                $Item | Add-Member -Type NoteProperty -Name Drive -Value "$drive`:\"
                $Item | Add-Member -Type NoteProperty -Name Target -Value $Target
                $OutputObject += $Item
            }
            else {
                Write-Output "$($Target.Substring(0, $Target.LastIndexOf('.'))) already exists- Updating."
                Write-Log -Message "$($Target.Substring(0, $Target.LastIndexOf('.'))) already exists- Updating."
                icacls "$Target" /grant $Username`:f /t
                Write-Output "Mounting VHD $Target"
                Write-Log -Message "Mounting VHD $Target" -LogPath $LogPath
                Mount-VHD $Target -ErrorAction SilentlyContinue
                $drive = (Get-DiskImage -ImagePath $Target | Get-Disk | Get-Partition).DriveLetter
                Start-Sleep 6
                if (($Drive | Measure-Object).count -gt 1) {
                    $Drive = $drive[1]
                }
                $Item = New-Object system.object
                $Item | Add-Member -Type NoteProperty -Name Drive -Value "$drive`:\"
                $Item | Add-Member -Type NoteProperty -Name Target -Value $Target
                $OutputObject += $Item
            }
        }
    }

    $OutputObject

}