3

For reasons of script portability, I need to dynamically load snap-ins in a PowerShell script. This is easily accomplished in PowerShell v2 with the Load-Module function. I need to run this particular script on a machine where I, for various reasons, do not want to install PowerShell v2, but have v1.

Is there a Load-Module equivalent in PowerShell v1?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
friism
  • 19,068
  • 5
  • 80
  • 116

2 Answers2

4

Do you mean Import-Module? If so, then it depends on how the module is defined. If it is a snapin DLL, then the snapin would need to be installed on the V1 machine and then you would use Add-PSSnapin. If it is in .psm1 file, then you would need to rename the file to .ps1 and then you could try to dot source it e.g. . .\mymodule.ps1. However if it uses any v2 feature like Export-ModuleMember, you will need to comment those out. And v1 wouldn't know what to do with a .psd1 file.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • I prefer not to install the snap-in on the v1 machine. Is there no way to just load the snap-in dynamically in v1? – friism Jan 13 '11 at 17:46
  • 3
    There's no way to import a snapin into your session in v1 without installing it (to the GAC, as an administrator) first. You can load assemblies and libraries, but the cmdlets won't show up except as .net classes. – Jaykul Jan 13 '11 at 18:35
  • The primary thing installation does is create a set of registry entries so that PowerShell can find the snapin dll. I guess you might be able to recreate the those registry entries with script (or a .reg file). – Keith Hill Jan 13 '11 at 19:24
2

Import-Module loads modules, and modules are a V2 only feature.

PowerShell V1 had "Snap-ins", written in a .NET language (C#, VB, ...) but not PowerShell script and loaded into a session with Add-PSSnapin (snap-ins are also supported in V2, but modules have a superset of snap-in capabilities so stick with modules unless it is impossible to upgrade to V2).

Richard
  • 106,783
  • 21
  • 203
  • 265