Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
February 21, 2023

More PowerShell Parameter Planning

In the last article, I started an exploration of parameter planning. The mechanics of adding a parameter to your PowerShell function is easy. But deciding exactly what you need requires planning and forethought. If you make smart decisions up front, your function will be easier to use and less error-prone.

We left off with this version of my function.

Function Get-FileByAge {
    [cmdletbinding()]
    Param(
        [alias("folder")]
        [string]$Path,
        [switch]$Recurse,
        [Int]$Hours,
        [string]$Property
    )
    Get-ChildItem -path $Path -file -Recurse:$Recurse |
    Where-Object { $_.$Property -ge (Get-Date).AddHours(-$Hours)}
}

The parameters let a user customize the behavior of the function. But you need to be careful. If you run help Get-FileByAge you’ll see that PowerShell automatically makes all the parameters, other than the switch, positional.

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.