0

As an example I have a module called web-utils, let's say I do some updates to it and I need to reload it like "module reload explained here". I have created a function

function Module-Reload([string]$name) {
  Remove-Module -Name $name -Force -WarningAction SilentlyContinue
  Import-Module -Name $name -Force -WarningAction SilentlyContinue
}

which throws an error as module name contain a hyphen, the question is: is there a way automatically escape hyphen in variables value? or is there another way of passing such parameters? As Import-Modules -Force web-utils works flawlessly only when it's typed manually in CLI. In case of doing it via function results in the following:

Import-Module : The specified module 'web`-utils' was not loaded because no
valid module file was found in any module directory.
At WindowsPowerShell\Aliases.ps1:35 char:3
+   Import-Module -Name $value -Force -WarningAction SilentlyContinue
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (web`-utils:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Andrius Solopovas
  • 967
  • 11
  • 41
  • There is nothing wrong with having hyphens in a module name. PowerShell throws the error shown in your question b/c it cannot find a module named ``web`-utils`` (with a backtick in the name). – Ansgar Wiechers Nov 06 '19 at 12:58
  • The string ``"web`-utils"`` reduces to `"web-utils"` *if* given as a literal, even though `-` does not actually need escaping. You likely have an `\`` too many somewhere where it's not actually required, or else a string is obtained from some source that doesn't go through PowerShell's parsing. – Jeroen Mostert Nov 06 '19 at 13:32

0 Answers0