Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

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?

like image 932
Peter Kellner Avatar asked May 29 '13 01:05

Peter Kellner


People also ask

How do I read all files in a directory in PowerShell?

PowerShell utilizes the “Get-ChildItem” command for listing files of a directory. The “dir” in the Windows command prompt and “Get-ChildItem” in PowerShell perform the same function. In this article, we have compiled examples to understand listing files in PowerShell.

How do I run multiple files in PowerShell?

To pass in multiple file names, you can list them comma-delimited in the command. This results in both directories a and b being copied into directory c .


1 Answers

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.

like image 132
Keith Hill Avatar answered Oct 04 '22 09:10

Keith Hill