GreenTriangle
GreenTriangle

Reputation: 2460

How do I perform a function on every item in a folder?

Very beginner question, but I'd appreciate some help with it.

I have a folder. I have a function, improve. I want to apply that function to every file within the folder. I've Googled this simple problem but none of the solutions I've found seem to work. What do I do?

Upvotes: 0

Views: 72

Answers (1)

Andy Arismendi
Andy Arismendi

Reputation: 52609

Something like this -

dir -path C:\stuff | ForEach-Object {improve $_}

If you make your function support object piping then you can do this -

dir -path C:\stuff | improve

Here's a simple example how to do that -

function improve ($FileItem) {
    process {
        if ($_) {$FileItem = $_}
        Write-Host $FileItem
    }
}

Upvotes: 2

Related Questions