IntroductionToWindowsPowerShell
.pdfAn Introduction to Windows PowerShell
Tasks |
|
Detailed Steps |
|
|
|
|
|
|
|
|
|
|
|
|
|
rktools.bak |
1237777 |
|
|
|
|
|
ntart.bak |
1227075 |
|
|
|
|
|
SET4.tmp |
1086058 |
|
|
|
|
|
Note: Cool, huh? Of course, you aren’t limited to returning the top 10 items: you can |
|
|||
|
|
specify any number you wish (for example, -first 37 returns the top 37 items). |
|
|||
|
|
Alternatively, use the –last parameter to specify items at the bottom of the list (in this |
|
|||
|
|
example, that would be files with the smallest file size). |
|
|||
|
|
b. Type this command into the command window, press ENTER, and see what you |
||||
|
|
|
get back: |
|
|
|
|
|
|
|
|||
|
|
|
get-childitem c:\restored | select-object name, length | |
|
||
|
|
|
sort-object length -descending | select-object -last 25 |
|
||
4. Viewing the |
|
|
|
|
|
|
|
Note: How did we know the name of the file size property in Windows PowerShell was |
|
||||
Properties and |
|
Length? Well, in this case we cheated a bit; after all, the Length property appears in |
|
|||
Methods of an |
|
the default output when using Get-ChildItem. But what if Length didn’t appear in the |
|
|||
Object |
|
default output? (After all, back in Exercise 1C we hinted at the fact that the default |
|
|||
|
|
output for Get-ChildItem does not include all the properties of a file.) What then? |
|
|||
|
|
Relax; Windows PowerShell makes it very easy to retrieve information about all the |
|
|||
|
|
properties and methods of an object. All you have to do is make a connection to the |
|
|||
|
|
object and then pipe the object to the Get-Member Cmdlet. For example, this |
|
|||
|
|
command returns a collection of items found in the folder C:\Restored, then pipes that |
|
|||
|
|
collection object to Get-Member: |
|
|
|
|
|
|
get-childitem c:\restored | get-member |
|
|||
|
|
a. To see what properties and methods are available to you when working with files |
||||
|
|
|
and folders other than the four we’ve already seen (Mode, LastWriteTime, Length, |
|||
|
|
|
and Name) simply type the following preceding command into the command |
|||
|
|
|
window and then press ENTER. |
|||
|
|
|
|
|||
|
|
|
get-childitem c:\restored | get-member |
|
Page 9 of 25
An Introduction to Windows PowerShell
Exercise 3
Grouping Items and Calculating Statistics
Scenario
Having taken a preliminary look at the recovered files you’ve decided that there’s no real rhyme or reason to the collection: these are simply a random series of files that the consultants were able to recover. Because of that, you’re leaning towards simply grouping the files by file type, putting all the Excel spreadsheets in one folder, all the Word documents in another folder, etc. That’s hardly the optimal way to sort and store files, but at least it provides a starting point for people who need to know, say, whether a particular PowerPoint presentation was recovered.
Of course, that only makes sense if there is a reasonable distribution of the file types; after all, if 99% of the files are Word documents and 1% are Excel spreadsheets then you haven’t really gained much by grouping files by file type. With that in mind, you’ve decided to retrieve the collection and group it by file extension, the better to see how many files of each type can be found in the folder.
Tasks |
Detailed Steps |
|
|
|
|
||
1. Grouping Items by |
Note: Of course, it’s one thing to say we’d like to group all the files by file extension; |
||
File Type |
it’s a whole ‘nother thing to actually do this, however. Or is it? |
||
|
As it turns out, it’s remarkably easy to group items based on a property value: that’s |
||
|
what the Group-Object Cmdlet lives for.For example, consider the following |
||
|
command, which retrieves a collection of all the files found in the folder C:\Restored |
||
|
and then pipes that collection to Group-Object: |
||
|
get-childitem c:\restored | group-object extension |
||
|
Note: What will Group-Object give us back? This: |
||
|
Count Name |
Group |
|
|
----- ---- |
----- |
|
|
21 |
.chm |
{api.chm, apps.chm,...} |
|
9 |
.bak |
{article.bak,...} |
|
3 |
.jpg |
{Ascent.jpg, Power.jpg,...} |
|
3 |
.gif |
{bullet.gif, exclam.gif,...} |
|
9 |
.wav |
{chimes.wav, chord.wav,...} |
|
63 |
.doc |
{Document_1.doc,...} |
|
42 |
.xls |
{Example_Spreadsheet_1.xls,... |
|
3 |
.mid |
{flourish.mid,...} |
|
80 |
.log |
{KB835221.log,...} |
|
53 |
.tmp |
{SET3.tmp, SET4.tmp,...} |
|
13 |
.txt |
{Text_File_1.txt,...} |
|
1 |
.hlp |
{wscript.hlp} |
Note: As you can see, there’s nothing too terribly complicated about using GroupObject; in this example all we had to do was call the Cmdlet followed by the property we wanted to group on (Extension).
a.Try typing in the command and then pressing ENTER; you should get the same thing we did.
get-childitem c:\restored | group-object extension
Page 10 of 25
An Introduction to Windows PowerShell
Tasks |
|
Detailed Steps |
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Note: And after you do that, try this variation. In this command we group the items by |
|
||||
|
|
file extension and then pass the grouped collection to the Sort-Object Cmdlet. In turn, |
|
||||
|
|
Sort-Object sorts the collection on the Count property. (When you group objects, the |
|
||||
|
|
grouped collection includes a property named Count which, as you can probably |
|
||||
|
|
guess, tells you the number of items in each grouping.) |
|
||||
|
|
b. Type the following command into the command window, press ENTER, and see |
|||||
|
|
|
what happens: |
||||
|
|
|
|
|
|||
|
|
|
get-childitem c:\restored | group-object extension | sort- |
|
|||
|
|
|
object count |
|
|||
|
|
c. Here’s an example of a fancier bit of grouping. We won’t explain this command in |
|||||
|
|
|
any detail, but it groups all the files first by the year they were created and then by |
||||
|
|
|
the month they were created. Type the following command into the command |
||||
|
|
|
window, press ENTER, and you’ll see what we’re talking about: |
||||
|
|
|
|
|
|||
|
|
|
get-childitem c:\restored | group-object |
|
|||
|
|
|
{$_.CreationTime.Year},{$_.CreationTime.Month} |
|
|||
2. Determining Overall |
|
|
|
|
|
|
|
|
Note: By now we should know that the folder C:\Restored contains a whole bunch of |
|
|||||
File Size |
|
files; what we don’t know is how many files, and – perhaps more important – how |
|
||||
|
|
much disk space those files are taking up. If we were to sit down and count the number |
|
||||
|
|
of files, then add the total amount of disk space being used, we’d come up with – well, |
|
||||
|
|
to tell you the truth, we’d come up with nothing, because counting up all those files |
|
||||
|
|
and adding up the file sizes would be too long and tedious a process. |
|
||||
|
|
Of course, while we don’t like long and tedious, Windows PowerShell doesn’t seem to |
|
||||
|
|
mind it. Therefore, why don’t we ask Windows PowerShell to use its Measure-Object |
|
||||
|
|
Cmdlet and calculate these statistics for us: |
|
||||
|
|
get-childitem c:\restored | measure-object length -average |
|
||||
|
|
-sum -maximum -minimum |
|
||||
|
|
As you can see, we’re – again – using Get-ChildItem to return a collection of all the |
|
||||
|
|
files found in the folder C:\Restored. We then pipe that collection to the Measure- |
|
||||
|
|
Object Cmdlet, which is requested to do two things: |
|
||||
|
|
|
|
|
|
||
|
|
|
|
|
|||
|
|
|
|
• Make its calculations based on the value of the Length property. |
|
||
|
|
|
|
• Return the average file size (-average); the total file sum (-sum); the size of |
|
||
|
|
|
|
|
the largest file (-maximum); and the size of the smallest file (-minimum). |
|
|
|
|
a. If you type the following command into the command window and then press |
|||||
|
|
|
ENTER |
||||
|
|
|
|
||||
|
|
|
get-childitem c:\restored | measure-object length -average |
|
|||
|
|
|
-sum -maximum -minimum |
|
|||
|
|
|
|||||
|
|
Note: you should get back information similar to the following: |
|
||||
|
|
Count |
: 300 |
|
|
||
|
|
Average : 100837.376666667 |
|
||||
|
|
Sum |
: 30251213 |
|
|
||
|
|
Maximum : 2698341 |
|
||||
|
|
Minimum : 26 |
|
||||
|
|
Property : Length |
|
||||
|
|
Of course, you can also use wildcards to focus in on a specific set of files. Remember |
|
Page 11 of 25
An Introduction to Windows PowerShell
Tasks |
|
Detailed Steps |
||||
|
|
|
|
|
|
|
|
|
those pesky .tmp files? This command returns the number and total size of all the .tmp |
|
|||
|
|
files in C:\Restored: |
|
|||
|
|
get-childitem c:\restored\*.tmp | measure-object length – |
|
|||
|
|
sum |
|
|
|
|
|
|
b. Type this command into the command window, press ENTER, and see what you |
||||
|
|
|
get back. |
|
|
|
|
|
|
|
|
||
3. Displaying a Single |
|
|
|
|
|
|
|
Note: Any time you call the Measure-Object Cmdlet you get back all the statistical |
|
||||
Property Value |
|
values, even those you didn’t ask for and that Measure-Object didn’t calculate for you. |
|
|||
|
|
a. For example, type the following command into the command window and then |
||||
|
|
|
press ENTER: |
|||
|
|
|
|
|||
|
|
|
get-childitem c:\restored | measure-object length -sum |
|
||
|
|
|
||||
|
|
Note: You should get back something similar to this, with empty placeholders for |
|
|||
|
|
properties like Average, Minimum, and Maximum: |
|
|||
|
|
Count |
: 79 |
|
|
|
|
|
Average |
: |
|
|
|
|
|
Sum |
: 3776752 |
|
|
|
|
|
Maximum |
: |
|
|
|
|
|
Minimum |
: |
|
|
|
|
|
Property : length |
|
|||
|
|
Note: That’s OK, albeit a tad-bit confusing. |
|
|||
|
|
But, hey, this is Windows PowerShell; you don’t have to settle for “OK.” If all you |
|
|||
|
|
want to get back is a value representing the sum then that’s all you should get back. |
|
|||
|
|
Take a look at this command (and don’t be put off by it; it’s nowhere near as |
|
|||
|
|
complicated as you might think): |
|
|||
|
|
(get-childitem c:\restored | measure-object length - |
|
|||
|
|
sum).sum |
|
|
|
|
|
|
Note: To explain how this command works it’s important that you understand the |
|
|||
|
|
purpose of parentheses in Windows PowerShell. As you doubtless recall from your |
|
|||
|
|
junior high math classes, an arithmetical equation such as this is difficult to interpret: |
|
|||
|
|
15 + 28 / 60 |
|
|
||
|
|
After all, do you add 15 and 28 and then divide that answer by 60? Or, do you divide |
|
|||
|
|
28 by 60 and then add that answer to 15? Needless to say, depending on which method |
|
|||
|
|
you choose you’ll get very different answers. |
|
|||
|
|
To help guard against misinterpretations, mathematicians use parentheses to indicate |
|
|||
|
|
which operations should be performed first. In this revised equation, the parentheses |
|
|||
|
|
tell us to first add 15 and 28, and then divide that answer by 60: |
|
|||
|
|
(15 + 28) / 60 |
|
|
||
|
|
Note: Parentheses serve a similar function in Windows PowerShell. Let’s take another |
|
|||
|
|
look at our command: |
|
|||
|
|
(get-childitem c:\restored | measure-object length - |
|
|||
|
|
sum).sum |
|
|
|
|
|
|
|
|
|
|
|
Page 12 of 25
An Introduction to Windows PowerShell
Tasks |
Detailed Steps |
|
|
|
Notice the parentheses surrounding this command: |
get-childitem c:\restored | measure-object length -sum
Note: Because this command is surrounded by parentheses that means we want Windows PowerShell to execute this command before it does anything else. In other words, Windows PowerShell is going to retrieve a collection of all the files in the folder C:\Restored, then pipe that collection to the Measure-Object Cmdlet. In turn, Measure-Object is going to calculate the sum of the Length property.
At this point in time we actually have a Windows PowerShell object (technically, a Microsoft.PowerShell.Commands.GenericMeasureInfo object). You can verify that for yourself by running the command and then piping the information to Get-Member:
Typically any time you pipe something to Measure-Object the Cmdlet performs its statistical wizardry and then reports back the results. That’s not what we want, however, and that’s why we enclosed the command in parentheses. Because we’re interested in only the Sum property we retrieve the object and then ask Windows PowerShell to echo back only the value of the Sum; that’s why we tacked the .sum on the end.
Make sense? Try typing the command into the command window and pressing ENTER; that should help. And then, for extra credit, use the preceding command as a template, and see if you can figure out how to return just the Average property.
Page 13 of 25
An Introduction to Windows PowerShell
Exercise 4
Deleting Files
Scenario
Earlier in this lab we complained about the large number of temporary files – files of no interest to us – that “litter” the folder C:\Restored. Because of that, we showed you how to use wildcard filtering to hide these files from the information returned by the Get-ChildItem Cmdlet. That’s fine, except for this: if these files truly aren’t of any use to us (and they aren’t) wouldn’t it be better to just get rid of them altogether? Why keep them if we don’t need or want them?
Hey, we wish we would have thought of that! With that in mind, in this exercise we’ll delete all the .tmp files found in the folder C:\Restored. In addition, we’ve determined that all the files in the folder that are larger than 1 megabyte are backup files that can safely be deleted as well. Therefore, after we’ve deleted all the .tmp files we’ll set our sights on deleting all the files bigger than 1 megabyte.
Tasks |
|
Detailed Steps |
||||
|
|
|
|
|
|
|
1. |
Deleting All Files |
|
|
|
|
|
|
Note: Removing all the .tmp files in a folder is easy; all we have to do is call the |
|
||||
|
with a Specified File |
|
Remove-Item Cmdlet followed by the list of files to be removed (in this case, we use |
|
||
|
Extension |
|
wildcards to indicate that we want to remove all the files with a .tmp file extension): |
|
||
|
|
|
a. Type the following command into the command window and then press ENTER. |
|||
|
|
|
|
|
||
|
|
|
|
remove-item c:\restored\*.tmp |
|
|
|
|
|
b. To verify that all the .tmp files have been deleted from C:\Restored type this |
|||
|
|
|
|
command into the command window and then press ENTER: |
||
|
|
|
|
|
||
|
|
|
|
get-childitem c:\restored\*.tmp |
|
|
2. |
Deleting All Files |
|
|
|
|
|
|
Note: In addition to deleting all the .tmp files, you’ve also determined that it’s safe to |
|
||||
|
Larger than a |
|
delete all the files larger than 1 megabyte; as near as you can tell, those are primarily |
|
||
|
Specified Size |
|
log files that, if needed, can be copied from another computer. So can we delete files |
|
||
|
|
|
greater than 1 megabyte using Windows PowerShell? Do you even need to ask? |
|
||
|
|
|
Note: Let’s take a look at the command that deletes all the files larger than 1 |
|
||
|
|
|
megabyte from the folder C:\Restored; after giving you a chance to look at the |
|
||
|
|
|
command we’ll explain how it works: |
|
||
|
|
|
get-childitem c:\restored | where-object {$_.length -gt |
|
||
|
|
|
1048576 } | foreach-object {remove-item $_.fullname} |
|
||
|
|
|
Note: In this command we’re introducing two new Cmdlets: Where-Object, which |
|
||
|
|
|
functions similar to a Where clause in a WMI query, and ForEach-Object, which |
|
||
|
|
|
enables us to create For Each loops. The way this works is that we use Get-ChildItem |
|
||
|
|
|
to return a collection of all the files in the folder C:\Restored. We then pipe that |
|
||
|
|
|
collection to Where-Object; the job of that Cmdlet is to weed out all files except those |
|
||
|
|
|
with a file size (Length) greater than 1 megabyte (1,048,576 bytes). Notice the syntax |
|
||
|
|
|
used with Where-Object: we call the Cmdlet followed by the filter criteria, which must |
|
||
|
|
|
be enclosed in curly braces. |
|
||
|
|
|
That’s a good idea; let’s spend a minute or two discussing the syntax of the filter itself. |
|
||
|
|
|
As you can see, we use the special variable $_ to represent the individual items in the |
|
||
|
|
|
collection. If you are familiar with VBScript then you probably are used to using code |
|
||
|
|
|
similar to this: |
|
||
|
|
|
|
|
|
|
Page 14 of 25
An Introduction to Windows PowerShell
Tasks |
Detailed Steps |
|
|
For Each objItem in colItems
Wscript.Echo objItem.Name
Next
Note: In the preceding code snippet, objItem is simply a variable that – each time we run through the loop – takes on the characteristics of the current item in the loop. The $_ variable serves a similar role in Windows PowerShell. In our Where-Object filter we’re simply asking Windows PowerShell to look at all the items in the collection and select only those individual items ($_) that have a length greater than 1,048,576 bytes. And yes, we also need to use –gt as the comparison operator. In Windows PowerShell you will typically use the following comparison operators:
|
Operator |
|
|
Definition |
|
|
|
|
|
||
|
|
|
|
|
|
|
-lt |
|
|
Less than |
|
|
|
|
|
||
|
|
|
|
|
|
|
-le |
|
|
Less than or equal to |
|
|
|
|
|
||
|
|
|
|
|
|
|
-gt |
|
|
Greater than |
|
|
|
|
|
||
|
|
|
|
|
|
|
-ge |
|
|
Greater than or equal to |
|
|
|
|
|
||
|
|
|
|
|
|
|
-eq |
|
|
Equal to |
|
|
|
|
|
||
|
|
|
|
|
|
|
-ne |
|
|
Not equal to |
|
|
|
|
|
||
|
|
|
|
|
|
|
-contains |
|
|
Determine elements in a group. This always returns |
|
|
|
|
|
Boolean $True or $False. |
|
|
|
|
|
||
|
|
|
|
|
|
|
-notcontains |
|
|
Determine excluded elements in a group. This always |
|
|
|
|
|
returns Boolean $True or $False. |
|
|
|
|
|
||
|
|
|
|
|
|
|
-like |
|
|
Like - uses wildcards for pattern matching |
|
|
|
|
|
||
|
|
|
|
|
|
|
-notlike |
|
|
Not Like - uses wildcards for pattern matching |
|
|
|
|
|
||
|
|
|
|
|
|
|
-match |
|
|
Match - uses regular expressions for pattern matching |
|
|
|
|
|
||
|
|
|
|
|
|
|
-notmatch |
|
|
Not Match - uses regular expressions for pattern |
|
|
|
|
|
matching |
|
|
|
|
|
|
|
Note. Interested in doing case-sensitive comparisons? Then simply insert the letter c after the hyphen in each operator. For example, the case-sensitive equals operator is this: -ceq.
Our Where-Object filter is similar to a SQL/WQL query such as this:
Select * From colItems Where Length > 1048576
After grabbing all the files larger than 1 megabyte we pipe this trimmed-down collection to the ForEach-Object Cmdlet. Let’s quickly review the syntax of this command:
foreach-object {remove-item $_.fullname}
Note: What we do here is call ForEach-Object, followed by the action we want to take on each item in the collection (this action must be enclosed in curly braces). We want to delete each item in the collection (that is, each file with a file size greater than 1 megabyte); therefore, we call the Remove-Item Cmdlet. In order to use Remove-Item to delete a file we need to pass the Cmdlet the complete path to that file; hence we use this syntax: {Remove-Item $_.FullName}, with $_ representing the individual items in
Page 15 of 25
An Introduction to Windows PowerShell
Tasks |
|
Detailed Steps |
|||
|
|
|
|
|
|
|
|
the collection, and FullName being the file property that contains the complete path. |
|
||
|
|
c. |
And you’re right: we have talked long enough about this one command, haven’t |
||
|
|
|
we? With that in mind, why not just type in the following, press ENTER, and then |
||
|
|
|
see what happens?: |
||
|
|
|
|
||
|
|
|
get-childitem c:\restored | where-object {$_.length -gt |
|
|
|
|
|
1048576 } | foreach-object {remove-item $_.fullname} |
|
|
3. Using File Size |
|
|
|
|
|
|
Note: Specifying a condition like $_.length –gt 1048576 is great, as long as you know |
|
|||
Designators |
|
that 1,048,576 bytes is equal to one megabyte. Of course, what if you were looking for |
|
||
|
|
files greater than 37 megabytes? In that case, you’d have to take 1,048,576 times 37, |
|
||
|
|
which would leave you with – look, wouldn’t it just be easier if Windows PowerShell |
|
||
|
|
understood terms like kilobyte, megabyte, and gigabyte? |
|
||
|
|
Well, guess what: it does. If you want to work with kilobytes, megabytes, and |
|
||
|
|
gigabytes all you have to do is use the appropriate designator: KB, MB, or GB. For |
|
||
|
|
example, this command also deletes all the files with a file size greater than 1 |
|
||
|
|
megabyte; notice, though, that cryptic numbers like 1048576 don’t appear anywhere |
|
||
|
|
in the command: |
|
||
|
|
get-childitem c:\restored | where-object {$_.length -gt |
|
||
|
|
1MB} | foreach-object {remove-item $_.fullname} |
|
||
|
|
Note: Here’s another example, a command that lists all the files in C:\Restored that |
|
||
|
|
have a file size greater than 100 kilobyte (KB). |
|
||
|
|
a. Type this command into the command window and then press ENTER: |
|||
|
|
|
|
||
|
|
|
get-childitem c:\restored | where-object {$_.length -gt |
|
|
|
|
|
100KB} |
|
|
|
|
b. |
Or try this. Type this command into the command window and then press |
||
|
|
|
ENTER: |
||
|
|
|
|
||
|
|
|
512KB + 512KB |
|
|
|
|
|
|||
|
|
Note: And then take a careful look at the answer you get back. Cool, huh? |
|
||
|
|
|
|
|
|
Page 16 of 25
An Introduction to Windows PowerShell
Exercise 5
Creating Folders
Scenario
If everything went according to plan in Exercise 4, the only files left in the folder C:\Restored are files we not only want to keep, but files we want to reorganize as well. In our case, we’ve decided that “reorganizing files” simply means storing files of the same type (based on file extension) in the same folder. That means we’re going to need to create a bunch of new folders, one for each file type (that is, one folder for .txt files, one folder for .doc files, etc.). In this exercise we’ll show you a barebones command for creating a single folder, then demonstrate a fancier command that can automatically create one folder for each unique file extension found in C:\Restored.
Tasks |
|
Detailed Steps |
||||
|
|
|
|
|
|
|
1. |
Create a New Folder |
|
|
|
|
|
|
Note: If you need to create a new folder then you need the New-Item Cmdlet. To |
|
||||
|
|
|
create a new folder all you have to do is call New-Item and specify two things: |
|
||
|
|
|
The path to the new folder. |
|
||
|
|
|
The –type parameter, with the type set to directory. Alternatively, you could create a |
|
||
|
|
|
new text file by setting the type to file. |
|
||
|
|
|
In other words, you need to use a command like this one, which creates the folder |
|
||
|
|
|
C:\Restored\Test: |
|
||
|
|
|
new-item c:\restored\test -type directory |
|
||
|
|
|
a. To create the new folder, type the preceding command into the command window |
|||
|
|
|
|
and then press ENTER. |
||
|
|
|
|
|
|
|
2. |
Verifying the |
|
|
|
|
|
|
Note: So did the new folder get created or not? One way to verify that is to use the |
|
||||
|
Existence of a Folder |
|
Test-Path Cmdlet. |
|
||
|
|
|
a. To determine whether or not the folder C:\Restored\Test exists type the following |
|||
|
|
|
|
command into the command window and then press ENTER: |
||
|
|
|
|
|
||
|
|
|
|
test-path c:\restored\test |
|
|
|
|
|
|
|||
|
|
|
Note: Windows PowerShell returns True if the folder exists and False if the folder |
|
||
|
|
|
does not exist. |
|
||
|
|
|
b. Here’s a cool thing you can do with Test-Path. Want to know if there are any .xls |
|||
|
|
|
|
files in the folder C:\Restored? Type this command into the command window, |
||
|
|
|
|
press ENTER, and see for yourself: |
||
|
|
|
|
|
||
|
|
|
|
test-path c:\restored\*.xls |
|
|
3. |
Auto-Create New |
|
|
|
|
|
|
Note: As you’ve seen, it’s pretty easy to create a single file folder using Windows |
|
||||
|
Folders Based on |
|
PowerShell. However, that’s of only marginal use to us at the moment. After all, we |
|
||
|
File Extension |
|
need to create a whole bunch of file folders, one for each file type (based on file |
|
||
|
|
|
extension) found in C:\Restored. That leaves us with two problems: determining the |
|
||
|
|
|
unique file types in the C:\Restored, and creating a corresponding folder for each file |
|
||
|
|
|
type. That sounds like a lot of tedious and time-consuming work, doesn’t it? It’s |
|
||
|
|
|
probably too much to ask Windows PowerShell to take care of this for us, isn’t it? |
|
||
|
|
|
Are you kidding; Windows PowerShell thrives on challenges such as this. To begin |
|
||
|
|
|
with, let’s show you how easy it is to retrieve a collection of the unique file types found |
|
||
|
|
|
in C:\Restored. |
|
||
|
|
|
|
|
|
|
Page 17 of 25
An Introduction to Windows PowerShell
Tasks |
Detailed Steps |
|
|
|
a. Type the following command into the command window and then press ENTER: |
get-childitem c:\restored | select-object extension | sort-object extension -unique
Note: You should get back something that looks like this: Extension
---------
.chm
.doc
.gif
.hlp
.jpg
.log
.mid
.txt
.wav
.xls
Note: Let’s explain how this command works. As you can see, we’ve actually used the Windows PowerShell pipeline to string together three separate commands. In the first, we use Get-ChildItem to return a collection of all the files found in the folder C:\Restored; that should be old hat to you by now. We then pipe the collection to the Select-Object Cmdlet, which filters out all the properties (and property values) except for Extension. Again, nothing there that you haven’t seen before.
We then pipe the collection of file extensions to this command:
sort-object extension -unique
Here we’re using Sort-Object to sort the collection by Extension (which happens to be the only property left in the collection). Notice, however, that we’ve tacked on the – unique parameter. The –unique parameter instructs Sort-Object to sort the collection and then extract only the unique items. What does that mean? Well, suppose our sorted collection looks like this, with a number of duplicate entries:
.doc
.doc
.doc
.txt
.txt
.xls
.xls
.xls
.xls
The –unique parameter weeds out the duplicate entries and leaves us with this:
.doc
.txt
.xls
Page 18 of 25