Public/Add-PsmDependency.ps1

function Add-PsmDependency {
  <#
    .Synopsis
      A function that creates a data file in the module's Data/ folder
    .DESCRIPTION
      A function that creates a data file in the module's Data/ folder
    .EXAMPLE
      Add-PsmDependency BITSTransfer
      Add-PsmDependency BITSTransfer -WhatIf
  #>

  [CmdletBinding(SupportsShouldProcess=$true)]
  param(
    [Parameter(
      Mandatory=$true, 
      ValueFromPipeline=$true,
      ValueFromPipelineByPropertyName=$true)]
    [string]$ModuleName,
    [Parameter(
      Mandatory=$false, 
      ValueFromPipeline=$true,
      ValueFromPipelineByPropertyName=$true)]
    [string]$Version
  )

  BEGIN{
    Write-Verbose "Starting Add-PsmDependency..."
    Confirm-IsInitializedModulePath
    $packageXml = Import-CliXml .\module-psd1.xml
  }#begin

  PROCESS{
    if ($psCmdlet.ShouldProcess(<# on target --> #>"$($packageXml.RootModule)", <# What if: Performing operation --> #>"Adding new dependency $ModuleName")) {    
      
      $dependency = Get-Module -ListAvailable *$ModuleName* | Select-Object -First 1

      if ($dependency -and ($dependency | Measure-Object).Count -gt 0) {
        
        $packageXml.RequiredModules += $dependency
        
        $packageXml | Export-CliXml .\module-psd1.xml
        
        Write-Verbose "Added dependency $dependency to module-psd1.xml"

      } else {
        throw "Module $ModuleName could not be found... Run: ``Get-Module -ListAvailable *modulename*`` "
      }
    }
  }#process

  END{
    Write-Verbose "Finished Add-PsmDependency..."
  }#end
}