Lab answer key: Using variables, arrays, and hash tables in PowerShell

Exercise 1: Working with variable types

Task 1: Use string variables

  1. On LON-CL1, select Start, and then enter powersh.
  2. In the results list, right-click Windows PowerShell or activate its context menu, and then select Run as administrator.
  3. To set the $logPath variable, at the Windows PowerShell prompt, enter the following command, and then press the Enter key:

    $logPath = "C:\Logs\"
    
  4. To display the variable type for $logPath, enter the following command, and then press the Enter key:

    $logPath.GetType()
    
  5. To review the properties and methods for the $logPath variable, enter the following command, and then press the Enter key:

     $logPath | Get-Member
    
  6. To set the $logFile variable, enter the following command, and then press the Enter key:

     $logFile = "log.txt"
    
  7. To add the $logFile variable to the $logPath variable, enter the following command, and then press the Enter key:

     $logPath += $logFile
    
  8. To review the contents of the $logPath variable, enter the following command, and then press the Enter key:

     $logPath
    
  9. To replace C: with D: in the $logPath value, enter the following command, and then press the Enter key:

     $logPath.Replace("C:","D:")
    
  10. To replace C: with D: in $logPath, enter the following command, and then press the Enter key:

     $logPath = $logPath.Replace("C:","D:")
    
  11. To review the contents of the $logPath variable, enter the following command, and then press the Enter key:

      $logPath
    
  12. Leave the Windows PowerShell prompt open for the next task.

Task 2: Use DateTime variables

  1. To set the $today variable equal to today’s date, at the Windows PowerShell prompt, enter the following command, and then press the Enter key:

    $today = Get-Date
    
  2. To review the variable type of the $today variable, enter the following command, and then press the Enter key:

    $today.GetType()
    
  3. To review the properties and methods for the $today variable, enter the following command, and then press the Enter key:

    $today | Get-Member
    
  4. To set a log file name based on the date, enter the following command, and then press the Enter key:

    $logFile = [string]$today.Year + "-" + $today.Month + "-" + $today.Day + "-" + $today.Hour + "-" + $today.Minute + ".txt"
    
  5. To calculate a date 30 days before today, enter the following command, and then press the Enter key:

    $cutOffDate = $today.AddDays(-30)
    
  6. To review users that have signed in for the last 30 days, enter the following command, and then press the Enter key:

    Get-ADUser -Properties LastLogonDate -Filter {LastLogonDate -gt $cutOffDate}
    
  7. Leave the Windows PowerShell prompt open for the next exercise.

Exercise 2: Using arrays

Task 1: Use an array to update the department for users

  1. To query all users in the Marketing department, at the Windows PowerShell prompt, enter the following command, and then press the Enter key:

    $mktgUsers = Get-ADUser -Filter {Department -eq "Marketing"} -Properties Department
    
  2. To identify how many users are in the $mktgUsers variable, enter the following command, and then press the Enter key:

    $mktgUsers.count
    
  3. To review the first user in $mktgUsers, enter the following command, and then press the Enter key:

    $mktgUsers[0]
    
  4. To modify the department to Business Development, enter the following command, and then press the Enter key:

    $mktgUsers | Set-ADUser -Department "Business Development"
    
  5. To review the Name and Department of users in the $mktgUsers variable, enter the following command, and then press the Enter key:

    $mktgUsers | Format-Table Name,Department
    
  6. Review the output and verify that the Department values in the $mktgUsers variable haven’t changed.

  7. To query all users in the Marketing department, at the Windows PowerShell prompt, enter the following command, and then press the Enter key:

    Get-ADUser -Filter {Department -eq "Marketing"}
    
  8. To query all users in the Business Development department, enter the following command, and then press the Enter key:

    Get-ADUser -Filter {Department -eq "Business Development"}
    
  9. Leave the Windows PowerShell prompt open for the next task.

Task 2: Use an array list

  1. To create an array list of computer names, at the Windows PowerShell prompt, enter the following command, and then press the Enter key:

    [System.Collections.ArrayList]$computers="LON-SRV1","LON-SRV2","LON-DC1"
    
  2. To verify that the $computers array list is not fixed-size, enter the following command, and then press the Enter key:

    $computers.IsFixedSize
    
  3. To add a computer name to the $computers array list, enter the following command, and then press the Enter key:

    $computers.Add("LON-DC2")
    
  4. To remove a computer name from the $computers array list, enter the following command, and then press the Enter key:

    $computers.Remove("LON-SRV2")
    
  5. To review the items in the $computers array list, enter the following command, and then press the Enter key:

    $computers
    
  6. Leave the Windows PowerShell prompt open for the next exercise.

Exercise 3: Using hash tables

Task 1: Use a hash table

  1. To create a hash table containing names and email addresses, at the Windows PowerShell prompt, enter the following command, and then press the Enter key:

    $mailList=@{"Frank"="Frank@fabriakm.com";"Libby"="LHayward@contso.com";"Matej"="MSTaojanov@tailspintoys.com"}
    
  2. To review the contents of the $mailList hash table, enter the following command, and then press the Enter key:

    $mailList
    
  3. To review the email address for Libby, enter the following command, and then press the Enter key:

    $mailList.Libby
    
  4. To update the email address for Libby, enter the following command, and then press the Enter key:

    $mailList.Libby="Libby.Hayward@contoso.com"
    
  5. To add a new name and email address to the hash table, enter the following command, and then press the Enter key:

    $mailList.Add("Stela","Stela.Sahiti")
    
  6. To remove Frank from the hash table, enter the following command, and then press the Enter key:

    $mailList.Remove("Frank")
    
  7. To review the contents of the $mailList hash table, enter the following command, and then press the Enter key:

    $mailList
    
  8. Close the Windows PowerShell prompt.