Skip to content

How can I use newly installed AWS Powershell modules without restarting Powershell?

0

Here is an example: I installed the CloudFormation module with: install-awsToolsModule -Name AWS.Tools.CloudFormation It installed. Then, I tried to run a command: $Stack = Get-CfnStack $StackName I got back, "Get-CfnStack: The 'Get-CfnStack' command was found in the module 'AWS.Tools.CloudFormation', but the module could not be loaded due to the following error: [Assembly with same name is already loaded] For more information, run 'Import-Module AWS.Tools.CloudFormation'." If I run that import command, I still get, "Import-Module: Assembly with same name is already loaded". If I try to use the Remove-Module command, I get, "Remove-Module: No modules were removed. Verify that the specification of modules to remove is correct and those modules exist in the runspace." So, how can I use cmdlets from this module without closing the Powershell window and starting a new instance of Powershell?

3 Answers
2

To use newly installed AWS PowerShell modules without restarting PowerShell, you can try the following approach:

  1. First, ensure that you're using the latest version of the AWS.Tools modules. You can update them using the AWS.Tools.Installer module if you haven't already done so.

  2. Instead of using Import-Module directly, try using the -Force parameter with the Import-Module command. This should force PowerShell to reload the module even if it's already loaded. The command would look like this:

    Import-Module AWS.Tools.CloudFormation -Force

  3. If the above doesn't work, you can try to remove the module from the current session and then reimport it. Use these commands:

    Remove-Module AWS.Tools.Common -Force Remove-Module AWS.Tools.CloudFormation -Force Import-Module AWS.Tools.CloudFormation

  4. If you're still encountering issues, you can try to clear the PowerShell module cache and then reimport the module:

    $env:PSModulePath -split ';' | Where-Object { $_ -like 'AWS.Tools' } | Get-ChildItem | Remove-Item -Recurse -Force Import-Module AWS.Tools.CloudFormation

These steps should allow you to use the newly installed AWS PowerShell modules without having to restart your PowerShell session. If you continue to experience issues, it may be related to how the modules are being loaded or potential conflicts with other installed modules. In such cases, you might need to investigate further or consider restarting PowerShell as a last resort.
Sources
AWS Tools for PowerShell is Now Generally Available with version 4.0
AWS Tools for PowerShell - Developing and Deploying .NET Applications on AWS

answered 2 years ago

EXPERT

reviewed 2 years ago

0

Hello!

To add to the recommended steps by re:Post Agent, ensure that your $Env:Path variable contains an entry for the directory where the PowerShell module is being installed to, such as "C:\Program Files\PowerShell\7".

To use newly installed AWS PowerShell modules without restarting PowerShell, you can try the following approach:

First, ensure that you're using the latest version of the AWS.Tools modules. You can update them using the AWS.Tools.Installer module if you haven't already done so.

Instead of using Import-Module directly, try using the -Force parameter with the Import-Module command. This should force PowerShell to reload the module even if it's already loaded. The command would look like this:

Import-Module AWS.Tools.CloudFormation -Force

If the above doesn't work, you can try to remove the module from the current session and then reimport it. Use these commands:

Remove-Module AWS.Tools.Common -Force Remove-Module AWS.Tools.CloudFormation -Force Import-Module AWS.Tools.CloudFormation

If you're still encountering issues, you can try to clear the PowerShell module cache and then reimport the module:

$env:PSModulePath -split ';' | Where-Object { $_ -like 'AWS.Tools' } | Get-ChildItem | Remove-Item -Recurse -Force Import-Module AWS.Tools.CloudFormation

These steps should allow you to use the newly installed AWS PowerShell modules without having to restart your PowerShell session. If you continue to experience issues, it may be related to how the modules are being loaded or potential conflicts with other installed modules. In such cases, you might need to investigate further or consider restarting PowerShell as a last resort.

answered 2 years ago

0

This is an in-process .NET assembly conflict, not a module-discovery or PATH problem.

Install-AWSToolsModule does more than copy the requested service module. By default it also updates the other installed AWS.Tools modules to the same version. The current PowerShell process can therefore still have an older AWS SDK assembly loaded while the module files on disk now refer to the newer version.

Remove-Module cannot fix that state. PowerShell's documentation explicitly says that it removes the cmdlets and other module members but does not unload a module's DLL. Import-Module -Force reimports a module; it does not replace an assembly that the process has already loaded. That is why both commands can still end with Assembly with same name is already loaded.

Once the conflict has occurred, use a new PowerShell process. A machine reboot is not required, and the terminal window itself can remain open. For example, from PowerShell 7:

pwsh -NoProfile -NoExit -Command "
    Import-Module AWS.Tools.CloudFormation
    Get-Command Get-CFNStack
"

For Windows PowerShell 5.1, use powershell.exe instead of pwsh. Exit that child process when finished to return to the original prompt.

For future installs or updates, run the installer in a separate clean process before loading AWS service modules in the process where you will use them. Also inspect the installed and loaded versions separately:

Get-Module AWS.Tools.*                       # loaded in this process
Get-Module AWS.Tools.* -ListAvailable        # installed on disk
Get-Module AWS.Tools.Common -ListAvailable |
    Select-Object Name, Version, Path

If you intentionally need to avoid updating all installed AWS service modules, Install-AWSToolsModule has -SkipUpdate; use it only after confirming that the requested module version is compatible with the installed AWS.Tools.Common version. -WhatIf can be used first to inspect the install operation.

Do not delete directories from $env:PSModulePath to solve this error. That changes the installation on disk and still cannot unload the assembly already in memory. Likewise, adding C:\Program Files\PowerShell\7 to $env:Path is unrelated: PowerShell finds modules through $env:PSModulePath, and the original error already says the command was found in AWS.Tools.CloudFormation.

Official references:

answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.