Lab answer key: Using PowerShell pipeline
Exercise 1: Enumerating objects
Task 1: Display a list of files on drive E of your computer
- On LON-CL1, select Start and then enter powersh.
- In the search results, right-click Windows PowerShell or activate its context menu, and then select Run as administrator.
-
In the Administrator: Windows PowerShell window, enter the following command, and then press the Enter key:
Get-ChildItem -Path E: -Recurse
-
In the console, enter the following command, and then press the Enter key:
Get-ChildItem -Path E: -Recurse | Get-Member
Note: Notice the GetFiles method in the list under TypeName: System.IO.DirectoryInfo.
-
In the console, enter the following command, and then press the Enter key:
Get-ChildItem -Path E: -Recurse | ForEach GetFiles
Task 2: Use enumeration to produce 100 random numbers
-
In the console, enter the following command, and then press the Enter key:
help *random*
Note: Notice the Get-Random command.
-
In the console, enter the following command, and then press the Enter key:
help Get-Random –ShowWindow
Note: Notice the -SetSeed parameter.
- Close the Get-Random Help window.
-
In the console, enter the following command, and then press the Enter key:
1..100
-
In the console, enter the following command, and then press the Enter key:
1..100 | ForEach { Get-Random –SetSeed $PSItem }
Task 3: Run a method of a Windows Management Instrumentation (WMI) object
- Close all applications other than the Windows PowerShell console.
-
In the console, enter the following command, and then press the Enter key:
Get-WmiObject –Class Win32_OperatingSystem -EnableAllPrivileges
-
In the console, enter the following command, and then press the Enter key:
Get-WmiObject –Class Win32_OperatingSystem -EnableAllPrivileges | Get-Member
Note: Notice the Reboot method.
Note: The following command will reboot the machine you run it on.
-
In the console, enter the following command, and then press the Enter key:
Get-WmiObject –Class Win32_OperatingSystem -EnableAllPrivileges | ForEach Reboot
Exercise 1 results
After completing this exercise, you should have created commands that manipulate multiple objects in the pipeline.
Exercise 2: Converting objects
Task 1: Update Active Directory user information
Note: In this lab, long commands typically display on several lines. This helps to prevent unintended line breaks in the middle of commands. However, when you enter these commands, you should enter them as a single line. That line might wrap on your screen into multiple lines, but the command will still work. press the Enter key only after entering the entire command.
- Sign in to the LON-CL1 as Adatum\Administrator with the password Pa55w.rd.
- On LON-CL1, select Start, and then enter powersh.
- In the search results, right-click Windows PowerShell or activate its context menu, and then select Run as administrator.
-
In the Administrator: Windows PowerShell window, enter the following command, and then press the Enter key:
Get-ADUser -Filter * -Properties Department,City | Where {$PSItem.Department -eq ‘IT’ -and $PSItem.City -eq ‘London’} | Select-Object -Property Name,Department,City| Sort Name
-
In the Administrator: Windows PowerShell window, enter the following command, and then press the Enter key:
Get-ADUser -Filter * -Properties Department,City | Where {$PSItem.Department -eq ‘IT’ -and $PSItem.City -eq ‘London’} | Set-ADUser -Office ‘LON-A/1000’
-
In the Administrator: Windows PowerShell window, enter the following command, and then press the Enter key:
Get-ADUser -Filter * -Properties Department,City,Office | Where {$PSItem.Department -eq ‘IT’ -and $PSItem.City -eq ‘London’} | Select-Object -Property Name,Department,City,Office | Sort Name
Task 2: Generate files listing the Active Directory users in the IT department
-
In the console, enter the following command, and then press the Enter key:
help ConvertTo-Html –ShowWindow
- Close the ConvertTo-Html Help window.
-
In the console, enter the following command, and then press the Enter key:
Get-ADUser -Filter * -Properties Department,City,Office | Where {$PSItem.Department -eq 'IT' -and $PSItem.City -eq 'London'} | Sort Name | Select-Object -Property Name,Department,City,Office | ConvertTo-Html –Property Name,Department,City -PreContent Users | Out-File E:\UserReport.html
-
To review the HTML file, enter the following command, and then press the Enter key:
Invoke-Expression E:\UserReport.html
-
In the console, enter the following command, and then press the Enter key (this will automatically open a web browser window):
Get-ADUser -Filter * -Properties Department,City,Office | Where {$PSItem.Department -eq 'IT' -and $PSItem.City -eq 'London'} | Sort Name | Select-Object -Property Name,Department,City,Office | Export-Clixml E:\UserReport.xml
- Review the report displayed on the web browser page and then close the web browser window.
-
In the console, enter the following command, and then press the Enter key:
Get-ADUser -Filter * -Properties Department,City,Office | Where {$PSItem.Department -eq 'IT' -and $PSItem.City -eq 'London'} | Sort Name | Select-Object -Property Name,Department,City,Office | Export-Csv E:\UserReport.csv
- Open File Explorer, in the File Explorer window, navigate to E:\, right-click UserReport.csv or activate its context menu, select Open with, and then select Notepad.
- In the Notepad window, review the content of the file.
Exercise 2 results
After completing this exercise, you should have queried Active Directory users, made changes to information about them, as well as converted Active Directory user objects to different data formats.