sql_dummy
sql_dummy

Reputation: 745

PowerShell : Is it mandatory to save script module in same name as directory

In regarding to Modules, Doc mentions its preferable

Save a PowerShell script with a .psm1 extension. Use the same name for the script and the directory where the script is saved.

To install and run your module, save the module to one of the appropriate PowerShell paths, and use Import-Module.

PowerShell paths -> located in the $env:PSModulePath

I haven't followed them and saved Script Module chart_gui.psm1 in one of a local folder and I could still Import-Module and call functions in it but Remove-Module is throwing error.

Import-Module 'H:\path_x\chart_gui.psm1'
#Call the function
$selectedCharts = selectCharts     
Remove-Module 'H:\path_x\chart_gui.psm1'

my Error:

Remove-Module : No modules were removed. Verify that the specification of modules to remove is correct and those modules exist in the runspace.

my $env:PSModulePath

PS C:\Users\xxx> $env:PSModulePath
C:\Users\xxx\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\Windo
wsPowerShell\v1.0\Modules

Upvotes: 1

Views: 372

Answers (2)

mklement0
mklement0

Reputation: 439822

Remove-Module isn't designed to accept file paths in order to identify the module to be removed.

Instead, use one of the following methods:

# By simple name.
# If your module is just a stand-alone *.psm1 file, the module name
# is the base name of that file (the file name without extension).
Remove-Module -Name chart_gui  # -Name is optional

# Example of a fully qualified module name, which assumes a module 
# that has a manifest file (*.psd1).
# Again, the base name of that file is implicitly the module name, and,
# typically, modules with *.psd1 files are placed in folders of the same
# name.
# Use the values specific to your module.
# To eliminate all ambiguity, you can also add a 'Guid' key with the GUID
# from your manifest.
Remove-Module -FullyQualifiedName @{ ModuleName = 'chart_gui'; RequiredVersion = '1.0.0' }

# By PSModuleInfo object, as reported by Get-Module or Import-Module -PassThru
Get-Module -Name chart_gui | Remove-Module  # -Name is optional
  • Typically, only one module with a given name is loaded in a session, but it is possible to load multiple ones side by side. That's when you need the -FullyQualifiedName parameter (which both Get-Module and Remove-Module support) in order to disambiguate.

  • A simple way to avoid ambiguity is to use -PassThru when you call Import-Module (with an explicit path), which outputs a System.Management.Automation.PSModuleInfo instance describing the module, which you can later pass to Remove-Module.

# Load (import) the module; -PassThru passes the
# imported module through as a PSModuleInfo object, which you can later pass
# to Remove-Module.
$module = Import-Module -PassThru 'H:\path_x\chart_gui.psm1'

# ... work with the module

# Unload it again.
$module | Remove-Module

Upvotes: 2

sql_dummy
sql_dummy

Reputation: 745

This may not still be the appropriate way but Ican avoid error by using

Remove-Module 'chart_gui'

Upvotes: 0

Related Questions