I have written 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 Import-Module only applies inside that script's scope - it does not import the module in the global scope. Is there any way to make a script import a module so that it stays around after the script finishes?
Note: dot-sourcing like so: . Reload-Module MyModuleName
does not work.
To declare a PowerShell global variable, simply use the below syntax. $global: myVariable ="This is my first global variable." If you choose not to give any value to it, you can explicitly assign a null value to it.
-ModuleInfoSpecifies the module objects to remove. Enter a variable that contains a module object (PSModuleInfo) or a command that gets a module object, such as a Get-Module command. You can also pipe module objects to Remove-Module .
To run a script on one or many remote computers, use the FilePath parameter of the Invoke-Command cmdlet. The script must be on or accessible to your local computer. The results are returned to your local computer.
To run a script, type the full name and the full path to the script file. To run a script in the current directory, type the path to the current directory, or use a dot to represent the current directory, followed by a path backslash ( . \ ).
From the 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's 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 parameter, which is a little 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
Note: the above help snippets are from v3.0 which is what I have installed on my system. The v2.0 help is available at http://msdn.microsoft.com/en-us/library/windows/desktop/dd819454.aspx. I'd heartily recommend getting PowerShell v3.0 if you can, if only because of the new ISE.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With