Make a PowerShell script run cmdlets in the global scope

I wrote the following PowerShell script:

function Reload-Module ([string]$moduleName) {
    $module = Get-Module $moduleName
    Remove-Module $moduleName -ErrorAction SilentlyContinue
    Import-Module $module
}

The only problem with this script is that the Import-Module is used only in the script area - it does not import the module into the global area. Is there a way to make the script import the module so that it remains after the script finishes?

Note: point-of-search looks like this: . Reload-Module MyModuleNamedoes not work.

+5
source share
1 answer

From Powershell Help:

-Global [<SwitchParameter>]
Imports modules into the global session state so they are available to all commands in the session. By 
default, the commands in a module, including commands from nested modules, are imported into the 
caller session state. To restrict the commands that a module exports, use an Export-ModuleMember 
command in the script module.

The Global parameter is equivalent to the Scope parameter with a value of Global.


Required?                    false
Position?                    named
Default value                False
Accept pipeline input?       false
Accept wildcard characters?  false

v3 also adds the -Scope option, which is a bit more general:

-Scope <String>
Imports the module only into the specified scope.

Valid values are:

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

-- Local: Available only in the current scope.

By default, the module is imported into the current scope, which could be 
a script or module.

This parameter is introduced in Windows PowerShell 3.0.

Required?                    false
Position?                    named
Default value                Current scope
Accept pipeline input?       false
Accept wildcard characters?  false

. v3.0, . v2.0 http://msdn.microsoft.com/en-us/library/windows/desktop/dd819454.aspx. PowerShell v3.0, , - ISE.

+4

All Articles