In this exercise, we explore the use of the
Start Windows PowerShell by using
Start Run Windows PowerShell
. The PowerShell prompt will
Use the
gal where-object {$_.definition -match "get-childitem"}
The results from the previous command show three aliases defined for the Get-ChildItem cmdlet, as shown here:
CommandType Name Definition ----------- ---- ---------- Alias gci Get-ChildItem Alias ls Get-ChildItem Alias dir Get-ChildItem
Using the gci alias for the Get-ChildItem cmdlet, obtain a listing of files and folders contained in the root directory. This is shown here:
gci
To identify large files more quickly, pipe the output to a Where-Object cmdlet, and specify the gt argument with a value of 1,000 to evaluate the length property. This is shown here:
gci where-object {$_.length -gt 1000}
To remove the cluttered data from your Windows PowerShell window, use cls to clear the screen. This is shown here:
cls
Use the Get-Alias cmdlet to resolve the cmdlet to which the cls alias points. You can use the gal alias to avoid typing get-alias if you wish. This is shown here:
gal cls
Use the Get-Alias cmdlet to resolve the cmdlet to which the mred alias points. This is shown here:
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
gal mred
It is likely that no mred alias is defined on your machine. In this case, you will see the following error message:
Get-Alias : Cannot find alias because alias 'mred' does not exist. At line:1 char:4 + gal <<<< mred
Use the Clear-Host cmdlet to clear the screen. This is shown here:
clear-host
Use the
Get-Member
cmdlet to retrieve a list of properties and
get-childitem get-member -membertype property
The output from the above command is shown here. Examine the output, and identify a property that could be used with a Where-Object cmdlet to find the date that files have been modified.
Name MemberType Definition ---- ---------- ---------- Attributes Property System.IO.FileAttributes Attributes {get;set;} CreationTime Property System.DateTime CreationTime {get;set;} CreationTimeUtc Property System.DateTime CreationTimeUtc {get;set;} Directory Property System.IO.DirectoryInfo Directory {get;} DirectoryName Property System.String DirectoryName {get;} Exists Property System.Boolean Exists {get;} Extension Property System.String Extension {get;} FullName Property System.String FullName {get;} IsReadOnly Property System.Boolean IsReadOnly {get;set;} LastAccessTime Property System.DateTime LastAccessTime {get;set;} LastAccessTimeUtc Property System.DateTime LastAccessTimeUtc {get;set;} LastWriteTime Property System.DateTime LastWriteTime {get;set;} LastWriteTimeUtc Property System.DateTime LastWriteTimeUtc {get;set;} Length Property System.Int64 Length {get;} Name Property System.String Name {get;}
Use the Where-Object cmdlet and choose the LastWriteTime property. This is shown here:
get-childitem where-object {$_.LastWriteTime}
Use the up arrow and bring the previous command back up onto the command line. Now specify the gt argument and choose a recent date from your previous list of files, so you can ensure the query will return a result. My command looks like the following:
get-childitem where-object {$_.LastWriteTime -gt "12/25/2006"}
Use the up arrow and retrieve the last command. Now direct the Get-ChildItem cmdlet to a specific folder on your hard drive, such as C:\fso, which may have been created in the step-by-step exercise from Chapter 1. You can, of course, use any folder that exists on your machine. This command will look like the following:
get-childitem "C:\fso" where-object {$_.LastWriteTime -gt "12/25/2006"}
Once again, use the up arrow and retrieve the last command. Add the recurse argument to the Get-ChildItem cmdlet. If your previous folder was not nested, then you may want to change to a different folder. You can, of course, use your Windows folder, which is rather deeply nested. I used my VBScript workshop folder, and the command is shown here (keep in mind that this command has wrapped and should be interpreted as a single line):
get-childitem -recurse "d:\vbsworkshop" where-object {$_.LastWriteTime -gt "12/25/2006" }
This concludes this step-by-step exercise. Completed commands for this exercise are in the
StepByStep.txt
file.