Mike Bailey
Mike Bailey

Reputation: 12817

Installing PowerShell Module that supports PowerShell 2.0 and 3.0

I have a PowerShell module that will be installed across a number of servers. The module itself utilizes PowerShell 2.0 features but needs to be able to be imported in both PowerShell 2.0 environments and PowerShell 3.0 environments.

I would like to install a single module into the PSModulePath that can support PowerShell 2.0 or 3.0. As it stands, the 2.0 module manifest looks like this:

@{
    ModuleToProcess = "Foo.psm1" # Specific to PowerShell 2.0
    ModuleVersion = "1.0"
    GUID = "8C0AB478-81F7-4B54-B440-521201EAEC1C"
    PowerShellVersion = "2.0"
    FunctionsToExport = "*"
    CmdletsToExport = "*"
    AliasesToExport = "*"
    VariablesToExport = "*"
}

Unfortunately, in 3.0 they removed ModuleToProcess and replaced it with RootModule:

@{
    RootModule = "Foo.psm1" # Specific to PowerShell 3.0
    ModuleVersion = "1.0"
    GUID = "8C0AB478-81F7-4B54-B440-521201EAEC1C"
    PowerShellVersion = "2.0"
    FunctionsToExport = "*"
    CmdletsToExport = "*"
    AliasesToExport = "*"
    VariablesToExport = "*"
}

My modules are currently deployed so they look something like:

C:\FooModules
  Foo-v1.0
    Foo
      Foo.psm1
      Foo.psd1
  Foo-v1.3
    Foo
      Foo.psm1
      Foo.psd1

And my PSModulePath is configured such that it contains C:\FooModules\Foo-v1.0 and C:\Foomodules\Foo-v1.3.

This works perfect if I target only 2.0 or only 3.0. But it is the case that I need to support both simultaneously. Is there any way to do this, short of writing a whole mess of preprocessing to install a 2.0 or 3.0 specific manifest? I really would not like to have to do such a thing.

Upvotes: 2

Views: 1382

Answers (1)

Frode F.
Frode F.

Reputation: 54971

There's no required change between a v2 manifest and a v3 manifest as far as I know. Use the v2 manifest to keep backwards-compatibility.

ModuleToProcess works just fine in v3.

Note: In Windows PowerShell 2.0, this key was called "ModuleToProcess." You can use the "RootModule" parameter name or its "ModuleToProcess" alias.

Source: Get-Help New-ModuleManifest -Parameter RootModule

Upvotes: 6

Related Questions