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:

2An error occurred while creating the pipeline.
3    + CategoryInfo          : NotSpecified: (:) [],
4    RuntimeException
5    + 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:

2Collection is read-only.
3At line:1 char:1
4+  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

 1function Do-Something
 3    Param
 4    (
 5        [Alias("In")]
 6        [Alias("I")]
 7        [Parameter()]
 8        $Input
 9    )
10    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

 1function Do-Something
 3    Param
 4    (
 5        [Alias("In","I")]
 6        [Parameter()]
 7        $Input
 8    )
 9    return $Input


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

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

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