Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

8: Working with strings In this step, you will use different operators to deal with strings values in Windows PowerShell. 1. To create and initialize

8: Working with strings

In this step, you will use different operators to deal with strings values in Windows PowerShell.

1. To create and initialize two string variables named var1 and var2, type the following commands, pressing ENTER after each one.

PS >$var1 = "Hello

PS >$var2 = "world"

2. To use the plus (+) operator to concatenate the two string variables, type the following command, and then press ENTER.

PS >$var1 + $var2

The result of this operation is a new Hello World string.

Windows PowerShell defines the behavior of the + operator for numbers, strings, arrays and hash tables. Adding two numbers produces a numeric result following the numeric widening rules. Adding two strings performs a string concatenation, resulting in a new string, and adding two arrays joins the two arrays (array concatenation).

3. You can use the .NET String properties to inspect the string objects. To use the Length property to obtain the size in characters of the previous string concatenation, type the following command, and then press ENTER.

PS >($var1 + $var2).Length

4. Windows PowerShell also provides other kinds of binary operators, like comparison operators. To verify if two strings are equal, type the following command, and then press ENTER,

PS >"John Smith" -eq "John Sanders"

There are other comparison operators, like ne (not equals) gt(greater than), lt(less than), ge(greater than or equal) and le (less than or equals).

5. Formatting is a common task that can also be done in Windows PowerShell. To use a custom format to display 12.4 as 12.40, type the following command, and then press ENTER.

PS >"{0:f2}" -f 12.4

6. To display the same number as currency, and then to pad it to 10 characters aligned to the right, type the following command, and then press ENTER.

Use the vertical bars to see the added padding:

PS >"|{0,10:C}|" -f 12.4

The currency symbol configured in the current culture of the local machine is used.

7. Date and time formatting can also be done. To display only hours and minutes from the current date, type the following command, and then press ENTER.

PS >"{0:hh:mm}" -f (Get-Date)

Task 9: Creating a script file

Script files are used to store Windows PowerShell commands in a file, providing an easy way to run a list of commands. You only need to tell Windows PowerShell to run the script file.

In this step, you will learn how to create and run script files. To understand the reasons behind the security features of Windows PowerShell, you will be introduced to a Windows PowerShell security feature called execution policies. The execution policy enables you to determine which Windows PowerShell scripts (if any) will be allowed to run on your computer. Windows PowerShell has four different execution policies: Department of Computer Science Spring 2019

Restricted No scripts are allowed to run. Windows PowerShell can only be used in interactive mode.

RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run.

Unrestricted No restrictions; all Windows PowerShell scripts can be run.

SHOW INPUT AND OUTPUT FOR EVERY STEP, AS WELL AS A SCREEN SHOT OF THE RESULTS

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Objects And Databases International Symposium Sophia Antipolis France June 13 2000 Revised Papers Lncs 1944

Authors: Klaus R. Dittrich ,Giovanna Guerrini ,Isabella Merlo ,Marta Oliva ,M. Elena Rodriguez

2001st Edition

3540416641, 978-3540416647

More Books

Students also viewed these Databases questions

Question

1. Understand how verbal and nonverbal communication differ.

Answered: 1 week ago