I've seen on a lot of places (for example, here, here, or here) where people suggest to use
Get-ChildItem -Path "Some path" -Recurse | Remove-Item
to recursively delete a directory, but why would this work?
When I run Get-ChildItem -Path "foo" -Recurse | % { "$_" }
, I see directories were listed first, and then files follows.
It shows something like this:
1st_child_directory
2st_child_directory
...
1st_file_in_foo
2nd_file_in_foo
...
1st_file_in_1st_child_directory
2st_file_in_1st_child_directory
...
1st_file_in_2st_child_directory
...
And so on.
If these get piped into Remove-Item
in the same order, then Remove-Item
would remove directories first (which are still non-empty), and then remove files inside directories (but directories should already been removed), and this is a bit counter-intuitive...
So why would Get-ChildItem -Recurse | Remove-Item
work, and how does it work?