src/_Get-CciModulesRoot.ps1

function _Get-CciModulesRoot {
<#
.SYNOPSIS
    Returns the PSModulePath root to install into, creating it when absent.
.DESCRIPTION
    Windows PowerShell 5.1 and PowerShell 7 use different user module roots, so
    the CurrentUser root is taken from PSModulePath (first entry under the user
    profile) rather than hardcoded.
#>

    [CmdletBinding()]
    param([ValidateSet('CurrentUser','AllUsers')][string]$Scope = 'CurrentUser')

    if ($Scope -eq 'AllUsers') {
        $root = Join-Path $env:ProgramFiles 'WindowsPowerShell\Modules'
    }
    else {
        $root = $env:PSModulePath -split [IO.Path]::PathSeparator |
            Where-Object { $_ -and $_.StartsWith($env:USERPROFILE, [StringComparison]::OrdinalIgnoreCase) } |
            Select-Object -First 1
        if (-not $root) { $root = Join-Path $env:USERPROFILE 'Documents\WindowsPowerShell\Modules' }
    }
    if (-not (Test-Path $root)) { New-Item -ItemType Directory -Path $root -Force | Out-Null }
    return $root
}