1

I'm using powershell core, here is the output of $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.3
PSEdition                      Core
GitCommitId                    7.3.3
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

I am trying to define a function on the top level of a script and then use it inside a ForEach-Object block.

Basically, I expected this would work. And it does.

function CustomPrint {
    param($Item)
    Write-Output $Item.Name
}

Get-ChildItem "." -Recurse -Filter "*.txt" | ForEach-Object { &CustomPrint -Item $_ }

This prints

test1.txt
test2.txt
test3.txt

Just some text files I have inside the folder.

However, when I add -Parallel to the ForEach-Object, this happens:

function CustomPrint {
    param($Item)
    Write-Output $Item.Name
}

Get-ChildItem "." -Recurse -Filter "*.txt" | ForEach-Object -Parallel { &CustomPrint -Item $_ }
&: The term 'CustomPrint' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
&: The term 'CustomPrint' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
&: The term 'CustomPrint' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Is this expected behavior? I tried looking into the docs but couldn't find any reason why the -Parallel flag would work differently. Thanks in advance.

igg
  • 2,172
  • 3
  • 10
  • 33

0 Answers0