# Recursive Function

**URL:** <https://forums.powershell.org/t/recursive-function/6745>\
**Category:** PowerShell Help\
**Created:** [July 2, 2016, 9:03am UTC](https://forums.powershell.org/t/recursive-function/6745 "2016-07-02T09:03:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr-d-j-tracey](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@mr-d-j-tracey](https://forums.powershell.org/u/mr-d-j-tracey)\
**Post date:** [July 2, 2016, 9:03am UTC](https://forums.powershell.org/t/recursive-function/6745/1 "2016-07-02T09:03:04Z")

</div>

Hi there,

I’m a PowerShell beginner and was watching a PluralSight course on beginning programming, wherein it described the use of recursion within a function to iterate through a directory hierarchy of unknown depth and find all mp3 files. The tutor did this in language-agnostic pseudo-code:

```
//pseudo-code
function listMP3Files (string currentFolder)
     for each item in folder:currentFolder
           if (item is mp3)
                print(name of item)
           end if
           if (item is folder)
              listMP3Files(name of item)
            end if
        end for
end function
```

Now, i tried to replicate this in PowerShell with:

```
function get-allmp3s {
 Foreach ( $item in Get-ChildItem ) {
	if ( $item.Name -like "*.mp3") {
		Write-Host "$item.Name"
		}
	elseif ( $item.PSIsContainer -eq $True ) { 
                get-allmp3s 
                } 
	}
}
```

…but the script just continually loops through the first folder in the hierarchy (i found this my adding a “Write-Host $item” at the start of the foreach loop.

I am aware the i could use the -Recurse option on Get-ChildItem, but i’m trying to learn how calling a function recursively works.

Thanks for your help.

Dan

---

<div class="post-metadata">

**Author:** ![peter-jurgens-2](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@peter-jurgens-2](https://forums.powershell.org/u/peter-jurgens-2)\
**Post date:** [July 2, 2016, 9:23am UTC](https://forums.powershell.org/t/recursive-function/6745/2 "2016-07-02T09:23:37Z")

</div>

What you’re missing is a parameter to specify the path you want to search in. With your current function it is simply calling get-childitem in the current location, which will be the same each time you call it, hence it continually runs in the current folder.

```
Function Get-AllMp3s ($path=.\){
  Get-ChildItem $path | ForEach-Object {
    If($_.name like "*.mp3"){
      Write-Host $_.name
    ElseIf($_.PSISContainer){
      Get-AllMp3s $_.full name
    }
  }
}
```

This was just a simple modification to tour own script as an example, personally I’d write the function to output objects, not text to display, amongst other things.

In this specific example with PSv3 and up you have parameters on Get-ChildItem which make this task a lot simpler.

```
Get-ChildItem .\ -Recursive -Filter "*.mp3" -File
```

---

<div class="post-metadata">

**Author:** ![mr-d-j-tracey](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@mr-d-j-tracey](https://forums.powershell.org/u/mr-d-j-tracey)\
**Post date:** [July 2, 2016, 9:49am UTC](https://forums.powershell.org/t/recursive-function/6745/3 "2016-07-02T09:49:59Z")

</div>

Thanks Peter. Perfect.

---

<div class="post-metadata">

**Author:** ![mr-d-j-tracey](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@mr-d-j-tracey](https://forums.powershell.org/u/mr-d-j-tracey)\
**Post date:** [July 2, 2016, 10:07am UTC](https://forums.powershell.org/t/recursive-function/6745/4 "2016-07-02T10:07:39Z")

</div>

REMOVED - User Error!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/recursive-function/6745/5 "2024-05-16T20:40:58Z")

</div>


