Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Created April 12, 2019 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IISResetMe/cc61ead8b032aeed7818d49f874c26f6 to your computer and use it in GitHub Desktop.
Save IISResetMe/cc61ead8b032aeed7818d49f874c26f6 to your computer and use it in GitHub Desktop.
Get-ChildItem -Depth for PS 4.0
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