You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
New-Object doesn't work with classes in nested modules #2963
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
New-Object -TypeName Dog -ArgumentList 'Fido'is the proper (if verbose) way to call a constructor via New-Object; its concise equivalent is New-Object Dog Fido (yes, the quotes are optional here).
In your example, Dog("Fido") is not a method call; it's a syntax based on a misconception that's unfortunately very common and persists because it just so happens to work:
New-Object is a command and therefore accepts arguments in argument mode, not expression mode (you can only perform method calls in the latter).
Dog and ("Fido") are therefore separate positional arguments, which bind to -TypeName and -ArgumentList, respectively.
From what I can tell - at least as of PowerShell Core v6.0.2 - even the static ::new() method doesn't see classes from nested modules.
It looks like the crux of the issue is that nested modules are imported with the (equivalent of) Import-Module rather than using, rendering any of the nested module's classes inaccessible - whether via ::new() or via New-Object.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 23, 2023
Activity
xavylon commentedon Jun 14, 2017
Solution: The issue is that you must call the constructor via a method call when you invoke the New-Object cmdlet. See the attached example code.
mklement0 commentedon Apr 14, 2018
xavylon:
There is no syntax issue here:
New-Object -TypeName Dog -ArgumentList 'Fido'is the proper (if verbose) way to call a constructor viaNew-Object; its concise equivalent isNew-Object Dog Fido(yes, the quotes are optional here).In your example,
Dog("Fido")is not a method call; it's a syntax based on a misconception that's unfortunately very common and persists because it just so happens to work:New-Objectis a command and therefore accepts arguments in argument mode, not expression mode (you can only perform method calls in the latter).Dogand("Fido")are therefore separate positional arguments, which bind to-TypeNameand-ArgumentList, respectively.mklement0 commentedon Apr 14, 2018
June Blender (@juneb):
From what I can tell - at least as of PowerShell Core v6.0.2 - even the static
::new()method doesn't see classes from nested modules.It looks like the crux of the issue is that nested modules are imported with the (equivalent of)
Import-Modulerather thanusing, rendering any of the nested module's classes inaccessible - whether via::new()or viaNew-Object.microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.