Lab answer key: Using variables, arrays, and hash tables in PowerShell
Exercise 1: Working with variable types
Task 1: Use string variables
- On LON-CL1, select Start, and then enter powersh.
- In the results list, right-click Windows PowerShell or activate its context menu, and then select Run as administrator.
-
To set the
$logPath
variable, at the Windows PowerShell prompt, enter the following command, and then press the Enter key:$logPath = "C:\Logs\"
-
To display the variable type for
$logPath
, enter the following command, and then press the Enter key:$logPath.GetType()
-
To review the properties and methods for the
$logPath
variable, enter the following command, and then press the Enter key:$logPath | Get-Member
-
To set the
$logFile
variable, enter the following command, and then press the Enter key:$logFile = "log.txt"
-
To add the
$logFile
variable to the$logPath
variable, enter the following command, and then press the Enter key:$logPath += $logFile
-
To review the contents of the
$logPath
variable, enter the following command, and then press the Enter key:$logPath
-
To replace C: with D: in the
$logPath
value, enter the following command, and then press the Enter key:$logPath.Replace("C:","D:")
-
To replace C: with D: in
$logPath
, enter the following command, and then press the Enter key:$logPath = $logPath.Replace("C:","D:")
-
To review the contents of the
$logPath
variable, enter the following command, and then press the Enter key:$logPath
- Leave the Windows PowerShell prompt open for the next task.
Task 2: Use DateTime variables
-
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
-
To review the variable type of the
$today
variable, enter the following command, and then press the Enter key:$today.GetType()
-
To review the properties and methods for the
$today
variable, enter the following command, and then press the Enter key:$today | Get-Member
-
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"
-
To calculate a date 30 days before today, enter the following command, and then press the Enter key:
$cutOffDate = $today.AddDays(-30)
-
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}
-
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
-
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
-
To identify how many users are in the
$mktgUsers
variable, enter the following command, and then press the Enter key:$mktgUsers.count
-
To review the first user in
$mktgUsers
, enter the following command, and then press the Enter key:$mktgUsers[0]
-
To modify the department to Business Development, enter the following command, and then press the Enter key:
$mktgUsers | Set-ADUser -Department "Business Development"
-
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
-
Review the output and verify that the Department values in the
$mktgUsers
variable haven’t changed. -
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"}
-
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"}
-
Leave the Windows PowerShell prompt open for the next task.
Task 2: Use an array list
-
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"
-
To verify that the
$computers
array list is not fixed-size, enter the following command, and then press the Enter key:$computers.IsFixedSize
-
To add a computer name to the
$computers
array list, enter the following command, and then press the Enter key:$computers.Add("LON-DC2")
-
To remove a computer name from the
$computers
array list, enter the following command, and then press the Enter key:$computers.Remove("LON-SRV2")
-
To review the items in the
$computers
array list, enter the following command, and then press the Enter key:$computers
-
Leave the Windows PowerShell prompt open for the next exercise.
Exercise 3: Using hash tables
Task 1: Use a hash table
-
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"}
-
To review the contents of the
$mailList
hash table, enter the following command, and then press the Enter key:$mailList
-
To review the email address for Libby, enter the following command, and then press the Enter key:
$mailList.Libby
-
To update the email address for Libby, enter the following command, and then press the Enter key:
$mailList.Libby="Libby.Hayward@contoso.com"
-
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")
-
To remove Frank from the hash table, enter the following command, and then press the Enter key:
$mailList.Remove("Frank")
-
To review the contents of the
$mailList
hash table, enter the following command, and then press the Enter key:$mailList
-
Close the Windows PowerShell prompt.