3

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

codewario
  • 19,553
  • 20
  • 90
  • 159
kevins1966
  • 366
  • 2
  • 4
  • 19
  • 1
    Try setting `$PSModuleAutoLoadingPreference = 'All'` in your [profile script](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles) – Mathias R. Jessen Apr 20 '22 at 15:24
  • It should autoload. Is the module not in the module path `$env:PSModulePath -split ';'`? You could always import it in your `$profile`. – js2010 Apr 20 '22 at 16:27
  • @MathiasR.Jessen. Thanks for that advice. I just tried that but the import did not persist across sessions. – kevins1966 Apr 21 '22 at 11:14
  • @js2010 `$env:PSModulePath -split ';'` produces the following
    `C:\Users\<>\Documents\PowerShell\Modules C:\Program Files\PowerShell\Modules c:\program files\powershell\7\Modules C:\Program Files\WindowsPowerShell\Modules WindowsPowerShell\Modules C:\Program Files (x86)\WindowsPowerShell\Modules C:\Windows\system32\WindowsPowerShell\v1.0\Modules C:\Program Files\Microsoft Monitoring Agent\Agent\PowerShell\
    C:\Program Files (x86)\Adaptiva\AdaptivaClient\data\PSModules`
    – kevins1966 Apr 21 '22 at 11:25
  • @js2010.... this is a continuation of the above as I ran out of allowed text... Oddly it is a combination of Windows Powershell and Powershell paths, some of which don't exist and one of which is not even a path, so I am assuming it's a shortcut powersehll can read. The directory `WebAdministration` exists in the 7th of these (`C:\Windows\system32\WindowsPowerShell\v1.0\Modules`) which is a `Windows Powershell` directory as opposed to `Powershell`. Should `Powershell` be able to pick it up? – kevins1966 Apr 21 '22 at 11:26
  • @js2010 apologies for the formatting. Comments appear to be very limiting for that. – kevins1966 Apr 21 '22 at 11:30
  • @kevins1966 Just to be clear: _module imports are **not supposed to persist** across sessions_. – Mathias R. Jessen Apr 21 '22 at 11:37
  • @MathiasR.Jessen. I see. If that is the case, what is the difference between Powershell and Windows Powershell because for the latter I do not ned to run `import-Module WebAdministration` for it to recognise 'get-command Get-Website` – kevins1966 Apr 21 '22 at 12:26

1 Answers1

2

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.

codewario
  • 19,553
  • 20
  • 90
  • 159