Get-ChildItemRecurse (modification of post by CrazyDave view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1115"></script>download | new post
A recursive function that allows the user to do a container search and specify the number of levels they would like to recurse. (Requires v2.0 CTP3 or later)
- function Get-ChildItemRecurse {
- <#
- .Synopsis
- Does a recursive search through a PSDrive up to n levels.
- .Description
- Does a recursive directory search, allowing the user to specify the number of
- levels to search.
- .Parameter path
- The starting path.
- .Parameter fileglob
- (optional) the search string for matching files
- .Parameter levels
- The numer of levels to recurse.
- .Example
- # Get up to three levels of files
- PS> Get-ChildItemRecurse *.* -levels 3
- .Notes
- NAME: Get-ChildItemRecurse
- AUTHOR: tojo2000
- #Requires -Version 2.0
- #>
- Param([Parameter(Mandatory = $true,
- ValueFromPipeLine = $false,
- Position = 0)]
- [string]$path = '.',
- [Parameter(Mandatory = $false,
- Position = 1,
- ValueFromPipeLine = $false)]
- [string]$fileglob = '*.*',
- [Parameter(Mandatory = $false,
- Position = 2,
- ValueFromPipeLine = $false)]
- [int]$levels = 0)
- if (-not (Test-Path $path)) {
- Write-Error "$path is an invalid path."
- return $false
- }
- $files = @(Get-ChildItem $path)
- foreach ($file in $files) {
- if ($file -like $fileglob) {
- Write-Output $file
- }
- #if ($file.GetType().FullName -eq 'System.IO.DirectoryInfo') {
- if ($file.PSIsContainer) {
- if ($levels -gt 0) {
- Get-ChildItemRecurse -path $file.FullName `
- -fileglob $fileglob `
- -levels ($levels - 1)
- }
- }
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository