Function Get-ChildItemToDepth {
<#
.SYNOPSIS
Returns child-items recursively to given depth.
.DESCRIPTION
Returns child-items recursively to given depth.
.PARAMETER Path
Path to start from
.PARAMETER Filter
Filter to use.
Defaults to "*"
.PARAMETER ToDepth
Specifies recursion depth.
Defaults to 0.
.PARAMETER FoldersOnly
Switch that specifies to only output folders.
Default is: output files and folders
.NOTES
Written by FurBall
.EXAMPLE
Get-ChildItemToDepth -Path 'D:\' -ToDepth 0 -FoldersOnly | select fullname
.EXAMPLE
Get-ChildItemToDepth -Path 'D:' -ToDepth 2 -FoldersOnly | ForEach-Object {
$path = $_.FullName
($path | Get-Acl).Access | ForEach-Object {
New-Object PSObject -Property @{
"Path" = $path;
IdentityReference = $_.IdentityReference;
FileSystemRights = $_.FileSystemRights;
IsInherited = $_.IsInherited;
}
}
} | ft
.LINK
http://www.fourcats.nl/Get-ChildItemToDepth.html
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][String]$Path = $PWD,
[Parameter(Mandatory=$false)][String]$Filter = "*",
[Parameter(Mandatory=$false)][Byte]$ToDepth = 0,
[Parameter(Mandatory=$false)][Byte]$CurrentDepth = 0,
[Switch]$FoldersOnly,
[Switch]$DebugMode
)
$CurrentDepth++
If ($DebugMode) { $DebugPreference = "Continue" }
Get-ChildItem $Path | ForEach-Object{
if ($FoldersOnly){
If ($_.PsIsContainer) {
$_ | Where-Object{ $_.Name -Like $Filter }
}
}
else{
$_ | Where-Object{ $_.Name -Like $Filter }
}
If ($_.PsIsContainer) {
If ($CurrentDepth -le $ToDepth) {
if ($FoldersOnly){
Get-ChildItemToDepth -Path $_.FullName -Filter $Filter -ToDepth $ToDepth -CurrentDepth $CurrentDepth -FoldersOnly
}
else{
Get-ChildItemToDepth -Path $_.FullName -Filter $Filter -ToDepth $ToDepth -CurrentDepth $CurrentDepth
}
}
}
}
}