Introducing Microsoft PowerShell


Key concepts: PowerShell Pipeline and PowerShell Objects

Source:  Microsoft

PowerShell is an object-oriented language, which makes it different than other command shells even though it may look similar. For example, if you type dir you get a list of files in the current directory, similar but not identical to that in a standard Windows command shell. However in PowerShell, dir (as well as ls, for those familiar with Linux or UNIX) is an alias for get-childitem which returns a collection of objects. By default, the output from dir only shows four properties of each file object: Mode, LastWriteTime, Length and Name. What if you wanted to know the CreationTime? How do you even know that there is a CreationTime property?

You can explore PowerShell objects using a couple of key features. One is the PowerShell pipeline. The pipeline character is | and using this passes the output to the following cmdlet. The format-list cmdlet will show all the properties, when used with the * argument. So by typing:

dir | format-list *

you can see all the properties of each file object, including CreationTime. You could also use the Select-Object (or select) cmdlet to display the exact properties you want, or the Where-Object (or where) cmdlet to filter the output. So this would show files larger than 100MB in or below the current folder and save the results in a text file:

dir -r | where length -gt 100mb | select fullname,length  | out-file bigfiles.txt

View All Photo Stories