Neil Barnwell
Neil Barnwell

Reputation: 42125

PowerShell command to parameterised PowerShell function?

I'm not so hot with PowerShell yet, but have managed to get this command to work quite nicely:

get-childitem "C:\Code\WC1" -Recurse | select-string "insert into\s+my_table"

Thing is, I know I'm going to struggle to remember this, so how can I make it into a function where the path supplied to get-childitem and the search regex are parameters?

I'm using PowerShell 2.0.

Upvotes: 1

Views: 239

Answers (2)

Djarid
Djarid

Reputation: 504

more commonly these days the parameters are being called after the function declaration e.g.

Function Find-Code {
    param([string] $path, [string] $pattern)
    get-childitem $path -Recurse | select-string $pattern
}

Upvotes: 2

Blair Conrad
Blair Conrad

Reputation: 241790

Function Find-Code([string] $path, [string] $pattern)
{
    get-childitem $path -Recurse | select-string $pattern
}

You can put this in your PowerShell Profile. An easy way to do this is to edit the $profile file (run something like notepad $profile from your PowerShell prompt) and just paste the text right in.

Upvotes: 1

Related Questions