Search code examples
powershellpowershell-3.0powershell-4.0

Powershell - "Expressions are only allowed as the first element of a pipeline"


How can I avoid this error in the below circumstance?

$codegenDir = "Z:\Desktop\Song-Renamer"
$PowerShellRepresentation = dir -path $MyMusicFolder -recurse -include *.mp3,*.m4a,*.wma,*.flac,*.ape | select -ExpandProperty FullName | $codegenDir\codegen.exe -s 10 20 | Out-String | ConvertFrom-Json

What puzzles me is if simply omit $codegenDir (see below), the code operates correctly. I think I understand the concept of placing the expression first (ahead of other items in the pipeline. But I'm not sure how to rearrange/split this code so the expression in question the Codegen.exe external commandline is the first item in the pipeline (and still be able to pass data to it via pipeline).

$PowerShellRepresentation = dir -path $MyMusicFolder -recurse -include *.mp3,*.m4a,*.wma,*.flac,*.ape | select -ExpandProperty FullName | .\codegen.exe -s 10 20 | Out-String | ConvertFrom-Json

Ideally, it would be nice to do this using the least amount of code as possible.


Solution

  • Give the following a shot (only difference is the &):

    $PowerShellRepresentation = dir -path $MyMusicFolder -recurse -include *.mp3,*.m4a,*.wma,*.flac,*.ape | select -ExpandProperty FullName | & $codegenDir\codegen.exe -s 10 20 | Out-String | ConvertFrom-Json
    

    Here's a link to a technet article about executing commands in powershell in different ways.