Skip to content

New-Object doesn't work with classes in nested modules #2963

@juneb

Description

@juneb

Steps to reproduce

  1. Define some PowerShell classes in a module file (ClassTester.psm1).
  2. In the manifest, leave RootModule empty and place the module name in the NestedModules key value.
ModuleToProcess = ''
NestedModules = 'ClassTester.psm1'
  1. In a script, use a Using module statement to import the module and its classes, use the New static method to instantiate one of the classes defined in ClassTester.psm1, and then use New-Object on the same class.
using module ClassTester
$myDog = [Dog]::New('Fido')
$myDog.Bark()

New-Object -TypeName Dog -ArgumentList 'Fluffy'
$myDog.Bark()

Expected behavior

Both the New method and New-Object cmdlet should work.

Actual behavior

New works, but New-Object generates an error.

'Woof!,' said Fido  # New method works
New-Object : Cannot find type [Dog]: verify that the assembly containing this type is loaded.
At C:\ps-test\Test-ClassTester.ps1:22 char:11
+ $fluffy = New-Object -TypeName Dog -ArgumentList 'Fluffy'
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

Environment data

Name                           Value
----                           -----
GitCommitId                    v6.0.0-alpha.14
PSRemotingProtocolVersion      2.3
BuildVersion                   3.0.0.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSVersion                      6.0.0-alpha
PSEdition                      Core
CLRVersion

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions