# Why does this get-childitem foreach work?

**URL:** <https://forums.powershell.org/t/why-does-this-get-childitem-foreach-work/20380>\
**Category:** PowerShell Help\
**Created:** [September 9, 2022, 6:15pm UTC](https://forums.powershell.org/t/why-does-this-get-childitem-foreach-work/20380 "2022-09-09T18:15:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nora](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nora/32/3930_2.png) [@nora](https://forums.powershell.org/u/nora)\
**Post date:** [September 9, 2022, 6:15pm UTC](https://forums.powershell.org/t/why-does-this-get-childitem-foreach-work/20380/1 "2022-09-09T18:15:10Z")

</div>

Hey all, another noob here. Having gone through the PS101 pages and mostly kinda-sorta understood what I read, I’m trying to do some scripting.

I have this script to recursively look through a path, find folders with inheritance removed, and print the path of just those folders:

```auto
$folders = Get-ChildItem -Recurse -Path "z:\temp"
foreach ($path in $folders)
{
  if ($path.PSIsContainer -eq $false)
  {
    continue
  }
  if ((get-acl $path.fullname).AreAccessRulesProtected -eq $true)
  {
    $path.fullname
  }
}

```

It works, which is great. But I don’t understand _why_ it works. If I run `$folders` I get the same output as running `Get-ChildItem -Recurse -Path "z:\temp"`. But how on Earth does the `foreach ($path in $folders)` work?

I don’t understand how we’re managing to grab just the path from the complete output of the Get-ChildItem command, especially because when I run `$path` I get complete results of Get-ChildItem for the last item that it processed.

Thank you for helping me out!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 9, 2022, 7:02pm UTC](https://forums.powershell.org/t/why-does-this-get-childitem-foreach-work/20380/2 "2022-09-09T19:02:48Z")

</div>

nora,  
Welcome to the forum. 👋🏽

> [@nora](#):
>
> It works, which is great. But I don’t understand _why_ it works. If I run `$folders` I get the same output as running `Get-ChildItem -Recurse -Path "z:\temp"`.

In PowerShell you can assign the output of any command to a variable. When you output this variable the output looks the same like you would run the command. That’s helpful when you use the output of the command more than once.

> [@nora](#):
>
> But how on Earth does the `foreach ($path in $folders)` work?

PowerShell created implicitly an array for you with the objects `Get-ChildItem` provided. The `foreach` loop enumerates each individual element of that array and assigns it to the loop variable you specified as `$path`.

> [@nora](#):
>
> I don’t understand how we’re managing to grab just the path from the complete output of the Get-ChildItem command, especially because when I run `$path` I get complete results of Get-ChildItem for the last item that it processed.

Since we work with objects and their properties in PowerShell you can access the individual properties with the so called dot notation `$path.fullname`. This way you get only the full path of the current object.

If you don’t need the raw data more than once your code can be shortened like this:

```auto
$folderList = 
Get-ChildItem -Path "z:\temp" -Recurse -Directory |
    Where-Object {
        (Get-Acl -Path $_.FullName).AreAccessRulesProtected -eq $true
    }
$folderList

```

And even if you want to use the result of `Get-ChildItem` more than once you could omit the first `if` condition with the switch parameter `-Directory` for your `Get-ChildItem`. This will list only folders - not files. The opposite would be the parameter `-File`. 😉

You should ALWAYS read the help for the cmdlets you’re about to use completely including the examples to learn how to use them:

> **[Get-ChildItem (Microsoft.PowerShell.Management) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.2)**
>
> The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the...

> **[about Foreach - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_foreach?view=powershell-7.2)**
>
> Describes a language command you can use to traverse all the items in a collection of items.

> **[Where-Object (Microsoft.PowerShell.Core) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/where-object?view=powershell-7.2)**
>
> The Where-Object cmdlet selects objects that have particular property values from the collection of objects that are passed to it. For example, you can use the Where-Object cmdlet to select files that were created after a certain date, events with a...

---

<div class="post-metadata">

**Author:** ![nora](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nora/32/3930_2.png) [@nora](https://forums.powershell.org/u/nora)\
**Post date:** [September 9, 2022, 7:14pm UTC](https://forums.powershell.org/t/why-does-this-get-childitem-foreach-work/20380/3 "2022-09-09T19:14:25Z")

</div>

Thank you, Olaf! I appreciate your help in this 🙂 I’m still confused with the `foreach ($path in $folders)` part. When you say:

> PowerShell created implicitly an array for you with the objects `Get-ChildItem` provided. The `foreach` loop enumerates each individual element of that array and assigns it to the loop variable you specified as `$path` .

How does it know which individual element of the array to specify as `$path`? `Get-ChildItem` returns more info than just the Name.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 9, 2022, 8:47pm UTC](https://forums.powershell.org/t/why-does-this-get-childitem-foreach-work/20380/4 "2022-09-09T20:47:24Z")

</div>

> [@nora](#):
>
> How does it know which individual element of the array to specify as `$path`? `Get-ChildItem` returns more info than just the Name.

The `foreach` loop iterates over all single elements in the array. The current element is saved in the variable `$path`. Inside the loop YOU specify to use the property `FullName` of the current object saved in `$path` with the dot notation `$path.FullName`.

You can output other properties to the console in the same way:

```auto
$folders = 
    Get-ChildItem -Path 'z:\temp' -Directory
foreach ($path in $folders) {
    "The name of the folder is '$($path.Name)' - the full path is '$($path.FullName)'"
}

```

Usually we try to have the result not with surrounding text. We want them as objects. This way we can use them for further steps

```auto
$folders = 
    Get-ChildItem -Path 'z:\temp' -Directory
foreach ($path in $folders) {
    [PSCustomObject]@{
        Name = $path.Name
        FullName = $path.FullName
        Parent = $path.Parent
        LastWriteTime = $path.LastWriteTime
    }
}

```

---

<div class="post-metadata">

**Author:** ![nora](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nora/32/3930_2.png) [@nora](https://forums.powershell.org/u/nora)\
**Post date:** [September 9, 2022, 11:31pm UTC](https://forums.powershell.org/t/why-does-this-get-childitem-foreach-work/20380/5 "2022-09-09T23:31:37Z")

</div>

Hi Olaf, I really appreciate your expanded explanation! I think I’m beginning to understand.

When I run `$folderList = Get-ChildItem -Path "z:\temp" -Recurse -Directory`, the `$folderList` variable contains all the objects along with their individual properties that are returned by the `Get-ChildItem` cmdlet.

In the `foreach`, PowerShell creates the variable `$path` automatically when the `foreach` loop runs, starting with the first value in the array.

Running `Get-ChildItem | Get-Member` I see that `.Name`, `.FullName`, and `PSIsContainer` are properties of System.IO.DirectoryInfo.

But what’s actually in the array? How is it structured? If I run `$folderList`, this is what I see:

```auto
PS Z:\temp> $folderlist

    Directory: Z:\temp

Mode LastWriteTime Length Name
---- ------------- ------ ----
da---- 12/28/2021 10:30 AM xltmoore2x
d----- 7/28/2022 8:02 AM Auditing

PS Z:\temp>

```

Is the array just a list of the directory names? And it’s looping through the directory names and I can query the directory properties using System.IO.DirectoryInfo members from `Get-ChildItem` because that’s where the data originally came from?

If this is essentially correct, then I’ve had what my colleague Scott would call an “Aha! moment.”

---

<div class="post-metadata">

**Author:** ![PoshDad](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/poshdad/32/2626_2.png) [@PoshDad](https://forums.powershell.org/u/PoshDad)\
**Post date:** [September 10, 2022, 1:53am UTC](https://forums.powershell.org/t/why-does-this-get-childitem-foreach-work/20380/6 "2022-09-10T01:53:17Z")

</div>

If you want to see all of the properties of the variable I would use this

```auto
$FolderList | Get-Member

```

Get-Member is your best friend whenever you are concerned about what properties & methods you can use.

You are correct about that output being in the array, the Foreach loop goes from start to finish assigning it to the variable in the left hand side(in your case it’s $Folder) then runs whatever you have in the scriptblock against it. Then it assigns it to the next item in the array and so on.

---

<div class="post-metadata">

**Author:** ![nora](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nora/32/3930_2.png) [@nora](https://forums.powershell.org/u/nora)\
**Post date:** [September 10, 2022, 1:56am UTC](https://forums.powershell.org/t/why-does-this-get-childitem-foreach-work/20380/7 "2022-09-10T01:56:41Z")

</div>

Thanks, PoshDad! Yeah, right now get-member is the jam. I don’t fully grasp what all I can do with the stuff, but I’m figuring it out 😃

---

<div class="post-metadata">

**Author:** ![PoshDad](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/poshdad/32/2626_2.png) [@PoshDad](https://forums.powershell.org/u/PoshDad)\
**Post date:** [September 10, 2022, 2:06am UTC](https://forums.powershell.org/t/why-does-this-get-childitem-foreach-work/20380/8 "2022-09-10T02:06:23Z")

</div>

Of course! Welcome to the forum.

Posting to the help forums here is a great way to start. I wish I would of done it when I started.

If you decide you’re really into it or this is something you want to pursue. Pick yourself up a copy of powershell in a month of lunches.

[Posh in a month of lunches](Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS [https://a.co/d/g2yiNs7](https://a.co/d/g2yiNs7))

That is an incredible place to start.

I would recommend using VS code as well as your editor with the powershell extension.

---

<div class="post-metadata">

**Author:** ![shikoo\_s](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/shikoo_s/32/3929_2.png) [@shikoo\_s](https://forums.powershell.org/u/shikoo_s)\
**Post date:** [September 10, 2022, 7:00pm UTC](https://forums.powershell.org/t/why-does-this-get-childitem-foreach-work/20380/9 "2022-09-10T19:00:11Z")

</div>

Hi,

If want to get an idea of what the book looks like “Powershell in a month of lunches”

this is an online copy:

> **[welcome · Learn PowerShell in a Month of Lunches: Fourth Edition, Covers...](https://livebook.manning.com/book/learn-powershell-in-a-month-of-lunches-linux-and-macos-edition/welcome/v-20/)**

---

<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:16pm UTC](https://forums.powershell.org/t/why-does-this-get-childitem-foreach-work/20380/10 "2024-05-16T20:16:40Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


