user3259248
user3259248

Reputation:

Powershell - piping to external program as arguments

I am dabbling with Powershell and attempting to replace the old console 'for' command. For instance, to encode a folder of *.WAV files using "FLAC.EXE" which is located on the path:

(Get-ChildItem)|Where-Object{$_.extension -eq ".wav"}|flac "$_.Name"

However I get a result where clearly Flac is not receiving the file name and only the literal string "$_.Name".

This is a very obvious problem I am sure, but I am still feeling my way along at this stage.

Upvotes: 0

Views: 104

Answers (1)

Keith Hill
Keith Hill

Reputation: 201602

Try it like this:

Get-ChildItem *.wav | Foreach-Object {flac $_.FullName}

The automatic variable $_ is typically only valid inside the context of a scriptblock that is part of a pipeline e.g. {...}.

Upvotes: 1

Related Questions