This page looks best with JavaScript enabled

Powershell 5: collection is read-only.

 ·  β˜• 2 min read  ·  πŸ€– Javy de Koning

Powershell 5: An error occurred while creating the pipeline

Today I was working with a Server 2016 TP5 server over PSR and I ran into this issue with one of my scripts:

Do-Something
An error occurred while creating the pipeline.
    + CategoryInfo          : NotSpecified: (:) [],
    RuntimeException
    + FullyQualifiedErrorId : RuntimeException

This script has been working fine on other systems so far so I was a bit confused. Google didn’t bring me much results so I decided to run the script locally on my Windows 10 machine and ended up with this:

Do-Something
Collection is read-only.
At line:1 char:1
+  Do-Something

The weird thing is that this script has been running fine on all other versions of Windows. I tried to run it on Windows 10 with Powershell version 2 and it worked fine as well.

After breaking down code I found the issue. Below is a simplified version of my function

function Do-Something
{
    Param
    (
        [Alias("In")]
        [Alias("I")]
        [Parameter()]
        $Input
    )
    return $Input
}

The alias definition above is a wrong example of defining an alias. Fortunately lower Powershell versions have been ‘forgiving’ and this command has been running fine due to that. The correct way to define an alias is detailed here by Microsoft. An example is provided below

function Do-Something
{
    Param
    (
        [Alias("In","I")]
        [Parameter()]
        $Input
    )
    return $Input
}

Bonus

As a bonus here is an example on how to find your scripts that have the same issue

Get-ChildItem $Home\PowershellTools -Recurse |
  select-string -pattern '(?smi)alias\S*\s*alias'

Javy de Koning
WRITTEN BY
Javy de Koning
Geek πŸ€“, Love sports πŸƒβ€β™‚οΈπŸ‹οΈβ€β™‚οΈ, Food πŸ›, Tech πŸ’», @Amsterdam ❌❌❌.