I’m building a small alternative to Get-ChildItem when working with the file system by using [System.IO]. I’m trying to find a way to use $PSBoundParameters.containskey in a more elegant way. The code I have below doesn’t really seem like the best way to be using it when working with multiple parameters that can all be on at the same time. I’ve seen examples using the add method and splatting but that was with Powershell cmdlets and not the underlying dot net. Any suggestions would be greatly appreciated.
function Get-EnumeratedItem { [CmdletBinding()] Param ( [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [Alias("FilePath")] [string[]]$Path, [string]$Filter, [switch]$Recurse ) Begin { #Converts relative path to absolute path $path = Convert-Path $path } Process { if (-not $PSBoundParameters.ContainsKey("Filter")) { if (-not $PSBoundParameters.ContainsKey("Recurse")) { Write-Verbose "Pulling folders in $path" $folders = [System.IO.Directory]::EnumerateDirectories($path, '*', 'TopDirectoryOnly') } else { Write-Verbose "Recursively pulling all folders in $path" $folders = [System.IO.Directory]::EnumerateDirectories($path, '*', 'AllDirectories') } } else { if (-not $PSBoundParameters.ContainsKey("Recurse")) { Write-Verbose "Pulling folders in $path" $folders = [System.IO.Directory]::EnumerateDirectories($path, "*$Filter*", 'TopDirectoryOnly') } else { Write-Verbose "Recursively pulling all folders in $path" $folders = [System.IO.Directory]::EnumerateDirectories($path, "*$Filter*", 'AllDirectories') } } foreach ($folder in $folders) { #more code } } End { } }