Exercise 2

  1. Create two similar text files (with one or two different lines). Compare them using diff.

    Click me for proposed solution
    ##Create 1st file
    New-Item -Path . -Name "file1.txt" -ItemType "file" -Value "Hello World.\nBonjour Monde!" -Force
    ##Create 2nd file
    New-Item -Path . -Name "file2.txt" -ItemType "file" -Value "Hello World!\nBonjour Monde." -Force
    ##Find the difference between two files
    diff -ReferenceObject(cat file1.txt) -DifferenceObject(cat file2.txt)
    

    InputObject    SideIndicator
    -----------    -------------
    Hello World!   =>           
    Bonjour Monde. =>           
    Hello World.   <=           
    Bonjour Monde! <=           
    

  2. What happens if you run:

    get-service | export-csv servicios.csv | out-file
    
    Why?
    Click me for proposed solution The output of the above command is:
    out-file: Cannot process argument because the value of the "path" argument is NULL. Change the value of the "path" argument to a non-null value.
    At line:1 char:42
    + get-service | export-csv servicios.csv | out-file
    +                                          ~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Out-File], PSArgumentNullException
        + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.OutFileCommand
    
    This is because export-csv servicios.csv creates the csv file and has null output, therefore out-file is not necessary.

  3. How would you create a file delimited by semicolons (;)? HINT: Use export-csv, but with an additional parameter.

    Click me for proposed solution
    ps | epcsv -delimiter ";" example.csv
    ##Same as
    Get-Process | Export-Csv -delimiter ";" example.csv
    

  4. Export-cliXML and Export-CSV modify the system, because they can create and overwrite files. Is there a parameter that prevents overwriting an existing file? Is there a parameter that allows the command to ask before overwriting a file?

    Click me for proposed solution To prevent overwriting the file if it already exists, the -NoClobber attribute can be used.

    Example: The following command will not overwrite the result from point 3

    ps | epcsv example.csv -NoClobber
    
    To ask for confirmation, use the -Confirm parameter.
    ps | epcsv -delimiter ";" example.csv -Confirm
    

  5. Windows uses regional settings, which includes the list separator. In English Windows, the list separator is a comma (,). How do you tell Export-CSV to use the system separator instead of the comma?

    Click me for proposed solution The cmdlet get-culture obtains system configuration information, such as language and writing information.

    ps | epcsv test2.csv -Delimiter ((get-culture).textInfo.listSeparator)
    

    Using cat (alias of Get-Content), we can see that the output uses ';' as the Spanish list separator.

    cat .\test2.csv
    

  6. Identify a cmdlet that allows generating a random number.

    Click me for proposed solution The cmdlet get-random returns a pseudo-random 32-bit integer.

  7. Identify a cmdlet that displays the current date and time.

    Click me for proposed solution The cmdlet get-date returns an object that represents the current date and time, which can be represented in various formats such as Windows or UNIX.

  8. What type of object does the cmdlet in question 7 produce?

    Click me for proposed solution Using the cmdlet gm, we can obtain the elements that make up the object and the type. As seen below, the object is part of the System package and its type is DateTime.
    Get-Date | gm
    
    TypeName: System.DateTime
    

  9. Using the cmdlet in question 7 and select-object, display only the day of the week as follows:

    DayOfWeek
    ---------
    Thursday
    

    Click me for proposed solution
    get-date | select -property "dayofweek"
    
    DayOfWeek
    ---------
    Friday
    

  10. Identify a cmdlet that displays information about patches (hotfixes) installed on the system.

    Click me for proposed solution The cmdlet that returns a list of system patches is:
    get-hotfix
    

  11. Using the cmdlet from question 10, display a list of installed patches. Then extend the expression to sort the list by installation date, and display on the screen only the installation date, the user who installed the patch, and the patch ID. Remember to examine the property names.

    Click me for proposed solution The cmdlet that returns a list of system patches is:
    get-hotfix | sort -property installedon | select -property installedon, installedby, hotfixid
    

  12. Complement the solution to question 11 so that the system sorts the results by the patch description, and includes the description, patch ID, and installation date in the listing. Write the results to an HTML file.

    Click me for proposed solution The cmdlet that returns a list of system patches is:
    get-hotFix | sort -property description | select -property hotfixid,description,installedon  | convertto-html | Out-File hotfix.html
    

  13. Display a list of the 50 newest entries from the System event log. Sort the list so that the oldest entries appear first, and entries produced at the same time should be sorted by index number. Show the index number, time, and source for each entry. Write this information to a plain text file.

    Click me for proposed solution The cmdlet that returns a list of system patches is:
    get-eventlog -newest 50 -logname system | sort -property index | sort -property timegenerated -descending | select -property index, timegenerated, source > exercise13.txt