Skip to content

Get-Module is polluting $error (from formatting script) (when strict mode on) #11909

Description

Seems this error comes from PowerShellCore_format_ps1xml.cs, from a handful of script blocks that look like this:

.AddScriptBlockColumn(@"
    if ($_.PrivateData -and $_.PrivateData.PSData)
    {
            $_.PrivateData.PSData.PreRelease
    }")

And of course PreRelease will not be defined for many (most?) modules that are out there.

Steps to reproduce

$error.Clear()
Set-StrictMode -Version Latest
New-ModuleManifest .\foo.psd1
ipmo .\foo.psd1
Get-Module foo | Out-Null
$error

Expected behavior

There should be no errors in $error, so there should be no output from the repro steps.

<no output>

Actual behavior

Error!

PropertyNotFoundException:
Line |
   4 |                                      $_.PrivateData.PSData.PreRelease
     |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The property 'PreRelease' cannot be found on this object. Verify that the property exists.

Although it doesn't show up in normal output, if you look into$error, it's there, making you wonder what went wrong. If you are looking into $error, you are probably trying to troubleshoot some problem, and you only want to see "real" errors in $error, and not be distracted or confused by things that just look like bugs in PowerShell.

Looking at it another way: I didn't run some command with -ErrorAction SilentlyContinue, and I didn't see an error from the command... so why in the world is an error showing up in $error? (That's why I described it as "polluting $error"--it seemingly is not an error; just the result of some internal PS bug scribbling useless and distracting information into it.)

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0-preview.7
PSEdition                      Core
GitCommitId                    7.0.0-preview.7
OS                             Microsoft Windows 10.0.19570
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions