Introduction to PowerShell Class Scripting
I know you’ve heard me go on and on about the importance of working with objects in the PowerShell pipeline. It is this paradigm that puts “Power” in the name. It is this paradigm that makes a command like this easy to create.
PS C:\> Get-ChildItem -path C:\work -file | Sort-Object -Property Length -Descending | Select-Object -first 10
Directory: C:\work
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 10/13/2022 11:48 AM 47148155 m6.mp4
-a--- 1/3/2023 5:43 PM 11534336 NetTrace.etl
-a--- 12/28/2020 2:51 PM 10194432 micro.exe
-a--- 5/16/2022 10:34 AM 6310547 Wait.zip
-a--- 1/3/2023 6:00 PM 4539591 trace.xml
-a--- 9/12/2022 12:35 PM 3142809 sales.json
-a--- 2/17/2022 2:26 PM 1065877 c.html
-a--- 2/18/2022 11:07 AM 805801 sample.zip
-a--- 10/29/2020 5:44 AM 792064 nano.exe
-a--- 2/17/2022 2:26 PM 321365 hv.html
The command works because each part of the pipelined expression works with a different part of the underlying file object. You should be able to look at the expression and visualize what PowerShell is doing.
But there’s so much more that we can do. We aren’t limited to objects from cmdlets. PowerShell makes it easy to customize the output.
Want to read the full issue?