MakePsModuleDirFromBuildDir.ps1

<#
  Written and shared by Microsoft employee Matthew Reynolds in the spirit of "Small OSS libraries, tool, and sample code" OSS policy
  MIT license https://github.com/MatthewMWR/MeasureTrace/blob/master/LICENSE
#>

param($Source = $psscriptroot, $InstallScope = 'CurrentUser', $Filter = '*.psd1')

foreach($moduleFile in Get-CHildItem $Source -Recurse -Filter $Filter){
    $tempModDir = (Join-Path $env:TEMP $moduleFile.BaseName)
    if(Test-Path $tempModDir -ErrorAction SilentlyContinue){ rd $tempModDir -Force -Recurse -ErrorAction Stop }
    Copy-Item -Path $moduleFile.Directory -Destination $tempModDir -Recurse
    $targetInstallDir = if([string]::IsNullOrEmpty($InstallScope)){
        $null
    } elseif ($InstallScope -eq 'CurrentUser'){
        $env:psmodulepath -split ';' | ?{ $_.StartsWith($env:userprofile)  } | select -First 1
    }
    elseif ($InstallScope -eq 'Machine'){
        Join-Path $pshome "Modules"
    }
    else{
        throw "Only blank, CurrentUser, or Machine are valid for InstallScope"
    }
    if(-not [string]::IsNullOrEmpty($targetInstallDir ) ){
        Remove-Item (Join-Path $targetInstallDir $moduleFile.BaseName) -Recurse -Force -ErrorAction SilentlyContinue
        New-Item $targetInstallDir -ItemType Directory -ErrorAction SilentlyContinue
        Copy-Item $tempModDir $targetInstallDir -Container -Recurse
    }
}