Using variables, arrays, and hash tables in PowerShell
This lab should take approximately 45 minutes to complete.
Scenario
You’re preparing to create scripts to automate server administration in your organization. Before you begin, you want to practice working with variables, arrays, and hash tables.
Objectives
After completing this lab, you’ll be able to:
- Work with variable types.
- Use arrays.
- Use hash tables.
Lab setup
Virtual machines: AZ-040T00A-LON-DC1, AZ-040T00A-LON-SVR1, and AZ-040T00A-LON-CL1
Username: Adatum\Administrator
Password: Pa55w.rd
For this lab, you’ll use the available virtual machine environment. Before you begin the lab, complete the following steps:
- Open LON-DC1 and sign in as Adatum\Administrator with the password Pa55w.rd.
- Repeat step 1 for LON-SVR1 and LON-CL1.
Exercise 1: Working with variable types
Exercise scenario 1
You first plan to practice working with different types of variables.
The main tasks for this exercise are:
- Use string variables.
- Use DateTime variables.
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
$logPathvariable, 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
$logPathvariable, enter the following command, and then press the Enter key:$logPath | Get-Member -
To set the
$logFilevariable, enter the following command, and then press the Enter key:$logFile = "log.txt" -
To add the
$logFilevariable to the$logPathvariable, enter the following command, and then press the Enter key:$logPath += $logFile -
To review the contents of the
$logPathvariable, enter the following command, and then press the Enter key:$logPath -
To replace C: with D: in the
$logPathvalue, 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
$logPathvariable, 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
$todayvariable 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
$todayvariable, enter the following command, and then press the Enter key:$today.GetType() -
To review the properties and methods for the
$todayvariable, 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
Exercise scenario 2
Now that you’ve practiced using different types of variables, you want to work with arrays.
The main tasks for this exercise are:
- Use an array to update the department for users.
- Use a generic list.
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
$mktgUsersvariable, 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
$mktgUsersvariable, 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
$mktgUsersvariable 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 a generic list
Note:
ArrayListis deprecated in favor of the strongly typed genericList[T]collection. For new scripts, use[System.Collections.Generic.List[string]]instead.
-
To create a generic list of computer names, at the Windows PowerShell prompt, enter the following command, and then press the Enter key:
[System.Collections.Generic.List[string]]$computers="LON-SRV1","LON-SRV2","LON-DC1" -
To verify that the
$computerslist is not fixed-size, enter the following command, and then press the Enter key:$computers.IsFixedSize -
To add a computer name to the
$computerslist, enter the following command, and then press the Enter key:$computers.Add("LON-DC2") -
To remove a computer name from the
$computerslist, enter the following command, and then press the Enter key:$computers.Remove("LON-SRV2") -
To review the items in the
$computerslist, 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
Exercise scenario 3
After using variables and arrays, you plan to practice working with hash tables. You want to learn how working with hash tables differs from arrays and generic lists.
The main task for this exercise is:
- Use a hash table.
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
$mailListhash 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
$mailListhash table, enter the following command, and then press the Enter key:$mailList -
Close the Windows PowerShell prompt.