YorSubs
YorSubs

Reputation: 4080

PowerShell Install-Module Local vs CurrentUser

Could someone explain this please?

Import-Module PSReadLine -Scope CurrentUser

Import-Module : Cannot validate argument on parameter 'Scope'.
The argument "CurrentUser" does not belong to the set "Local,Global"
specified by the ValidateSet attribute. Supply an argument that is in the
set and then try the command again.
At line:1 char:37
+     Import-Module PSReadLine -Scope CurrentUser
+                                     ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Import-Module], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ImportModuleCommand

But this works fine(!)

Install-Module PSScriptAnalyzer -Scope CurrentUser

And this works fine(!)

Import-Module PSReadLine -Scope Local

The help file for Install-Module doesn't even recognise the existence of Local|Global mentioned in the error. -Scope <String> Specifies the installation scope of the module. The acceptable values for this parameter are AllUsers and CurrentUser.

Upvotes: 2

Views: 25298

Answers (2)

Marcus N&#228;tteldal
Marcus N&#228;tteldal

Reputation: 225

For me and those who face similar issue, I had to add curly brackets "{}" around CurrentUser. Else nothing happened.

Example: Install-Module -Name SimplySql -Scope {CurrentUser}

Upvotes: 0

JosefZ
JosefZ

Reputation: 30238

These scopes differ substantially (excerpted from official learn.microsoft.com):

Import-Module -Scope

Specifies a scope into which this cmdlet imports the module.

The acceptable values for this parameter are:

Global. Available to all commands in the session. Equivalent to the Global parameter.

Local. Available only in the current scope.

By default, when Import-Module cmdlet is called from the command prompt, script file, or scriptblock, all the commands are imported into the global session state. You can use the -Scope parameter with the value of Local to import module content into the script or scriptblock scope.

When invoked from another module, Import-Module cmdlet imports the commands in a module, including commands from nested modules, into the caller's session state. Specifying -Scope Global or -Global indicates that this cmdlet imports modules into the global session state so they are available to all commands in the session.

Install-Module -Scope

Specifies the installation scope of the module. The acceptable values for this parameter are AllUsers and CurrentUser.

The AllUsers scope installs modules in a location that is accessible to all users of the computer:

$env:ProgramFiles\PowerShell\Modules

The CurrentUser installs modules in a location that is accessible only to the current user of the computer:

$home\Documents\PowerShell\Modules

When no Scope is defined, the default is set based on the PowerShellGet version.

  • In PowerShellGet versions 2.0.0 and above, the default is CurrentUser, which does not require elevation for install.
  • In PowerShellGet 1.x versions, the default is AllUsers, which requires elevation for install.

Upvotes: 8

Related Questions