Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell 7 import-module does not persist across sessions

If I open a new Powershell 7 session and run get-command Get-Website the response I get is

Get-Command: The term 'Get-Website' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

If I then run import-Module WebAdministration and again run get-command Get-Website, this time I get the expectefd output of

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Get-Website                                        1.0        WebAdministration

If I close the Powershell 7 session, open a new one and run get-command Get-Website, the new session does not recognise the command.

If I run a session of Windows Powershell, I do not have to import the module, the command is already there.

Anyone able to explain what is going on?

Thanks

like image 307
kevins1966 Avatar asked Jan 27 '26 09:01

kevins1966


1 Answers

You are not the only person having this problem. Unfortunately, I can't speak to whether any of the solutions there work, as neither the asker or anyone else has confirmed them.

Note: I had originally suspected this might be a problem with automatic importing through the WindowsCompatibility module, but it appears that it does not interfere with auto-importing of modules. In addition, as of PowerShell 7 the WindowsCompatibility module features are baked into PowerShell itself.

For whatever reason, WebAdministration may not be able to be automatically imported in PowerShell Core. There are a few reasons for this but they are mostly on the module side, and you can't change the behavior without modifying the module.

You can try setting $PSModuleAutoLoadingPreference = 'All' in your current PowerShell session, but generally that doesn't need to be changed from the default value of ModuleQualified. More information on $PSModuleAutoLoadingPreference can be found here.

If that doesn't work, you'll have to manually import the module in every session.


Fortunately, manually importing modules isn't required for the vast majority of them. For the ones which can't be automatically imported, you must use Import-Module MODULENAME in each new session. You can simplify this by adding the Import-Module cmdlet to one of the following profile locations:

$profile.CurrentUserCurrentHost
$profile.CurrentUserAllHosts
$profile.AllUsersCurrentHost
$profile.AllUsersAllHosts

For scripts, Import-Module should generally be done inside the script to prevent needing profiles in scenarios where profile loading is desired to be disabled such as when PowerShell is invoked in the following way: powershell.exe -NoProfile ....

In your case, it would like like so:

Import-Module WebAdministration

Here is some additional information about the $profile variable.

like image 101
Bender the Greatest Avatar answered Jan 29 '26 01:01

Bender the Greatest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!