Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Get-ChildItem -Depth for PS 4.0
function Get-ChildItemDeep
[Parameter(ParameterSetName='Items', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
# 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
# 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
You can’t perform that action at this time.