I am trying to import a Powershell module that's already installed but having an issue with passing the array from Get-Module and converting to the correct format to run Import-Module. To explain the first line, there are 2 modules that work AzureADPreview and AzureAD. This needs to work for both hence the wildcard.
if (Get-Module -ListAvailable -Name AzureAD*) {
$AzureModule = Get-Module -ListAvailable -Name AzureAD* | Select-Object Name -First 1| Format-Table -HideTableHeaders | Out-String
$AzureModule2 = $AzureModule2.Replace(' ', '')
Import-Module $AzureModule2
}else {Install-Module -Name AzureAD
Import-Module AzureAD}
$AzureModule2 | Out-file test.txt
The txt file contains blank lines top and bottom which I can't seem to clear.
Line | 4 | Import-Module $AzureModule2 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ | The specified module ' AzureAD ' was not loaded because no valid module file was found in any module directory.