MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))

In this exercise, we explore the use of the Get-ChildItem and Get-Member cmdlets in Windows PowerShell. You will see that it is easy to use these cmdlets to automate routine administrative tasks. We also continue to experiment with the pipelining feature of Windows PowerShell.

  1. Start Windows PowerShell by using Start | Run | Windows PowerShell. The PowerShell prompt will open by default at the root of your Documents And Settings.

  2. Use the Get-Alias cmdlet to retrieve a listing of all the aliases defined on the computer. Pipe this output to a Where-Object cmdlet. Specify a match argument against the definition property that matches the name of the Get-ChildItem cmdlet. The code is as follows:

    gal | where-object {$_.definition -match "get-childitem"}

  3. 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

  4. 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

  5. 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}

  6. To remove the cluttered data from your Windows PowerShell window, use cls to clear the screen. This is shown here:

    cls

  7. 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

  8. Use the Get-Alias cmdlet to resolve the cmdlet to which the mred alias points. This is shown here:

    gal mred

  9. 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

  10. Use the Clear-Host cmdlet to clear the screen. This is shown here:

    clear-host

  11. Use the Get-Member cmdlet to retrieve a list of properties and methods from the Get-ChildItem cmdlet. This is shown here:

    get-childitem | get-member -membertype property

  12. 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;}

  13. Use the Where-Object cmdlet and choose the LastWriteTime property. This is shown here:

    get-childitem | where-object {$_.LastWriteTime}

  14. 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"}

  15. 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"}

  16. 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" }

  17. This concludes this step-by-step exercise. Completed commands for this exercise are in the  StepByStep.txt file.

Категории