Introduction to Windows PowerShell

1 2 3 4 5 6 7 8 9 10 11 12 Page 2
Page 2 of 12

With no other arguments, Get-ChildItem emits File and Folder objects for all the files and subfolders in the current directory. If I type the command dir with no arguments, Get-ChildItem spits out a list of File and Folder objects, and because there is no pipe or output redirection, the results spill into the Windows PowerShell command window.

When objects land in the PowerShell window, PowerShell prints one line for each object, in a nice columnar format, listing the most important properties of each object. For File and Folder objects, this includes the Name, Length, LastWriteTime, and Mode properties. The resulting list looks like this:

Directory: C:\users\bknittel<br> Mode LastWriteTime Length Name<br> ---- ------------- ------ ----<br> d-r-- 6/2/2010 1:27 PM Contacts<br> d-r-- 6/15/2010 11:35 PM Desktop<br> d-r-- 6/14/2010 8:47 AM Documents<br> d-r-- 6/2/2010 1:27 PM Downloads<br> :<br> d-r-- 6/2/2010 1:27 PM Videos<br> d-r-- 6/23/2010 11:01 AM Virtual Machines<br> -a--- 8/21/2009 6:07 PM 1889 listprops.vbs<br> -a--- 8/20/2009 12:34 PM 1020 nets.vbs<br> -a--- 8/20/2009 11:54 AM 3668 tempfile.txt<br> -a--- 9/14/2009 7:13 PM 1605 test.vbs<br> -a--- 6/23/2010 2:19 PM 3650 x

The headings at the top of the columns are just the names of the properties that are being displayed. The listing would be similar for any other type of object a cmdlet generated.

The thing to ponder here is that the dir command in the regular Command Prompt environment generates text in a fixed format. The dir cmdlet in Windows PowerShell generates a list of File and Folder objects, which PowerShell formats into a text listing after the fact.

You can redirect the output of a PowerShell cmdlet to a file, using the familiar > character, and the same thing occurs: the stream of objects gets formatted into a nice, text listing.

The innovation in PowerShell is what happens when you use the pipe redirection symbol (|). PowerShell lets you direct a stream of objects from one cmdlet to another, and they can change the properties of and call methods on these objects, doing real work as they get passed along. It’s only when the objects finally hit the screen that text appears. One cmdlet might generate a list of objects representing files, computers, services, and network objects; the next in the pipeline might filter and pass on just the ones that are of interest; and the next might call methods to perform actions on the objects. This is a truly unique feature of Windows PowerShell.

So, to do what used to require a loop in WSH:

fso = CreateObject(“Scripting.FileSystemObject”)<br> for each file in fso.GetFolder(“c:\temp”).Files<br>    file.Delete<br> next

you can do with a single command line in Windows PowerShell:

dir c:\temp | remove-item

dir generates a list of File and Folder objects representing the contents of directory c:\temp and passes it to the remove-item command, which deletes the “real world” thing behind any object it’s passed.

1 2 3 4 5 6 7 8 9 10 11 12 Page 2
Page 2 of 12
7 inconvenient truths about the hybrid work trend
Shop Tech Products at Amazon