Created
April 12, 2019 15:05
-
-
Save IISResetMe/cc61ead8b032aeed7818d49f874c26f6 to your computer and use it in GitHub Desktop.
Get-ChildItem -Depth for PS 4.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ChildItemDeep | |
{ | |
param( | |
[Parameter(ParameterSetName='Items', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] | |
[string[]] | |
${Path}, | |
[Parameter(Position=1)] | |
[string] | |
${Filter}, | |
[Parameter(Mandatory=$true)] | |
[int] | |
${Depth} | |
) | |
# Make a copy of the params for `Get-ChildItem` | |
$GCIParams = @{} | |
foreach($ParamName in $PSBoundParameters.Keys.Where({$_ -ne 'Depth'})){ | |
$GCIParams[$ParamName] = $PSBoundParameters[$ParamName] | |
} | |
# Decrease depth for next call | |
$PSBoundParameters['Depth']-- | |
# Call Get-ChildItem | |
Get-ChildItem @GCIParams |ForEach-Object { | |
$_ | |
# Recurse if appropriate | |
if($_.PSIsContainer -and $Depth -gt 1){ | |
$PSBoundParameters['Path'] = $_.FullName | |
Get-ChildItemDeep @PSBoundParameters | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment