Peter Kellner
Peter Kellner

Reputation: 15498

How to Use Powershell to execute a program against all files in a directory

I'm using windows 8 pro and want to do something I'm hoping is very simple. I just want to execute one program against all the files of a certain type in a directory. no trees, just in the flat directory. In Linux I would:

find . -name 'exec c:\user\local\bin\myprog {} \;

I've literally spend a couple hours wrestling with power shell, running into policy problems, permissions, etc. Is there some simple way I can make this happen?

Upvotes: 47

Views: 50701

Answers (1)

Keith Hill
Keith Hill

Reputation: 201672

This is easy but different than using find e.g.:

Get-ChildItem -File | Foreach {c:\user\local\bin\myprog $_.fullname}

For doing stuff at the command line, aliases can make this a bit more terse:

ls -file | % {c:\user\local\bin\myprog $_.fullname}

PowerShell prefers commands that are narrow in focus but that can be composed together in a pipeline to provide lots of capability. Also, PowerShell pipes .NET objects e.g. Get-ChildItem pipes System.IO.FileInfo objects. You can then use commands like Foreach, Where, Select, Sort, Group, Format to manipulate the objects passed down the pipeline. If you have time, I recommend you checking out my free ebook Effective PowerShell.

Upvotes: 75

Related Questions