Great exercise to compare the simple version with the more technical method which leads to a great understanding of the mechanics. Really great tutorial series!
Yeah haha, as it says in the description: "I lied, there is no extra task! All the "physical stuff" of creating tools has been moved to the later episodes, so stick around for that."
Based on where you left off at 23:01, I immediately completed the task with ls | % { $_.GetType() } | where { $_.Name -eq ‘DirectoryInfo’} Of course it is not as elegant as your solution
Yep, that'll certainly leave you with only directories, really great progress! The Where works great, good job on that, I'm glad you're getting how properties work and everything! The only problem with doing the final task this way is that "% { $_.GetType() }" you have is throwing away the original file/directory objects. As soon as that foreach finishes, all it leaves you with are a bunch of Types, which you can't do file operations to. So yes, as you do, you can then go and filter those Type objects down to just the ones that say "DirectoryInfo" like you do, but in practice, you wouldn't actually be able to do anything with the result from this, because your original file objects were gone as soon as that foreach ran. This command just leaves you with a bunch of, identical, DirectoryInfo types, which you can't do any operations to. But we can fix that really easily! Instead of transforming all the file/directory objects into Type right at the beginning, by moving the ".GetType()" inside the Where, getting the _where_ to do it as part of its checking process, that will leave us with the real directory objects! ls | Where { $_.GetType().Name -eq 'DirectoryInfo' }
Great video! I tried your challenge and came up with another way to get the directory listing using the where command to query the Name. I actually thought that was where you were going but then you showed another method to emphasize a deeper understanding. This is what I found works too: ls | where { $_.GetType().Name -eq 'DirectoryInfo' }
Yes! That too will work, and I was hopeful that a few people would indeed find their own way by taking advantage of everything that's there. There's often many different ways you can approach something, and I knew that this was also a way you could go. I went for the [type] way because it's not something you'd necessarily think of, and as you say, it really hones in what that [type] means, that's the only reason I went for that. But anything that works, works :)