- Lab
- A Cloud Guru
Strings and Arrays in PowerShell Core for Linux
The string data type is probably the most used data type in PowerShell. From displaying messages, prompting for input, or sending data to files, it is almost impossible to write scripts without strings being involved. An array is a data structure designed to store a collection of items. The items can be the same type or different types. This hands-on lab will demonstrate the use of both strings and arrays in PowerShell for Linux.
Path Info
Table of Contents
-
Challenge
Perform a System Update, Register the MS RedHat Repository, and Install PowerShell
- Use the
yum
command to sync the package index files from their sources via the Internet.
sudo yum check-update
- Use the
yum
command to install the newest versions of all installed packages on CentOS.
sudo yum update
- Register the Microsoft RedHat repository.
curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
- Install PowerShell.
sudo yum install -y powershell
- Start PowerShell.
pwsh
- Use the
-
Challenge
Work with Strings in PowerShell
- Create a single quote string 'Hello PowerShell - Today is $(Get-Date)'.
'Hello PowerShell - Today is $(Get-Date)'
- Create a double quote string "Hello PowerShell - Today is $(Get-Date)".
"Hello PowerShell - Today is $(Get-Date)"
- Show the properties of the double quote string using
Get-Member
and a pipeline.
"Hello PowerShell - Today is $(Get-Date)" | Get-Member
- Create four variables (
domain
,firstname
,lastname
, anddepartment
) using the following information:- Domain - scriptingguru.com
- First name - John
- Last name - Smith
- Department - History
$domain = 'scriptingguru.com' $firstname = 'John' $lastname = 'Smith' $department = 'History'
- Using the previously entered variables, derive the following values using the string concatenation operator (
+
):- Name =
firstname lastname
- DisplayName =
firstname lastname (department)
- SamAccountName =
firstname.lastname
- EmailAddress =
[email protected]
- Name =
$firstname + ' ' + $lastname $firstname + ' ' + $lastname + ' (' + $department + ')' $firstname + '.' + $lastname $firstname + '.' + $lastname + '@' + $domain
- Once again using the previously entered variables, obtain the same values using PowerShell string expansion.
"$firstname $lastname" "$firstname $lastname ($department)" "$firstname.$lastname" "$firstname.$lastname@$domain"
-
Challenge
Work with Arrays in PowerShell
- Create an array named
A
that contains the seven numeric (int
) values of 29, 51, 6, 8, 19, 39, and 180.
$A = 29,51,6,8,19,39,180
- Create an array named
B
using the range operator (..
) containing the values 1 through 8.
$B = 1..8
- Create a strongly-typed 32-bit integer array named
ia
containing four integers (1148, 2195, 3376, and 9000).
[int32[]]$ia = 1148,2195,3376,9000
- Display all the elements in the array
$A
.
$A
- Display the first element in the array
$A
.
$A[0]
- Dispay the third element in the array
$A
.
$A[2]
- Recreate the array
$A
to contain the numbers 0 through 9 using the range operator (..
).
$A = 0 .. 9
- Display the last three elements of the array, using negative indexes.
$A[-1..-3]
- Exit PowerShell.
exit
- Create an array named
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.