Lab: Using variables, arrays, and hash tables in PowerShell
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.
Estimated time: 45 minutes
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, open a Windows PowerShell prompt.
- Create a variable
$logPath
that contains C:\logs. - For
$logPath
, identify the type of variable and the available properties and methods. - Create a variable
$logFile
that contains log.txt. - Update
$logPath
to include the contents of$logFile
. - Update the path stored in
$logPath
to use drive D instead of drive C. - Leave the Windows PowerShell prompt open for the next task.
Task 2: Use DateTime variables
- At the Windows PowerShell prompt, create a variable
$today
that contains today’s date. - For the variable
$today
, identify the type of variable and the available properties and methods. - Use the properties of
$today
to create a string in the format Year-Month-Day-Hour-Minute.txt, and store the value in$logFile
. - Create a variable
$cutOffDay
that contains the date 30 days before today. - Use Get-ADUser to query user accounts that have signed in since
$cutOffDay
. Filter by using the LastLogonDate property. - 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 an array list.
Task 1: Use an array to update the department for users
- Query all Active Directory Domain Services (AD DS) users in the Marketing department and place them in a variable named
$mktgUsers
. Include the Department property in the results. - Use
$mktgUsers
to identify the number of users in the Marketing department. - Display the first user in
$mktgUsers
and verify that the Department property is listed. - Pipe the users in
$mktgUsers
to Set-ADUser and update the department to Business Development. - Review the Department attribute in
$mktgUsers
to check if it has been updated. - Query all AD DS users in the Marketing department to verify that there are none.
- Query all AD DS users in the Business Development department and verify that it matches the previous count from the Marketing department.
- Leave the Windows PowerShell prompt open for the next task.
Task 2: Use an array list
- Create an arraylist named
$computers
with the values LON-SRV1, LON-SRV2, and LON-DC1. - Verify that
$computers
doesn’t have a fixed size. - Add LON-DC2 to
$computers
. - Remove LON-SRV2 from
$computers
. - Display the contents of
$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 array lists.
The main task for this exercise is:
- Use a hash table.
Task 1: Use a hash table
-
Create a hash table named
$mailList
with the following users and email addresses:- User Frank with the email address Frank@fabrikam.com
- User Libby with the email address LHayward@contoso.com
- User Matej with the email address MStojanov@tailspintoys.com
- Display the contents of
$mailList
. - Display the email address for Libby.
- Update the email address for Libby to Libby.Hayward@contoso.com.
- Add a new email address for Stela: Stela.Sahiti@treyresearch.net.
- Review the count of items in
$mailList
. - Remove Frank from
$mailList
. - Verify that Frank is removed.
- Close the Windows PowerShell prompt.