0

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.

Jason27300
  • 11
  • 2
  • 1
    As a general rule, formatting commands like ```Format-Table``` and ```Format-List``` should only be used for *displaying* data - e.g. to the console or to a log file, and not for processing data into another form. If you just want a single property from the matching modules you can do this: ```$AzureModule = Get-Module -ListAvailable -Name Azure* | Select-Object -ExpandProperty "Name" -First 1``` – mclayton Jul 04 '22 at 10:01
  • Works perfect and way less complicated. Thanks for the advice. – Jason27300 Jul 04 '22 at 10:46

0 Answers0