Question: Task 8: Working with strings In this step, you will use different operators to deal withstrings values in Windows PowerShell. 1. To create andinitialize two

Task 8: Working with strings

In this step, you will use different operators to deal withstrings values in Windows PowerShell. 1. To create andinitialize two string variables named var1 and var2, type thefollowing commands, pressing ENTER after each one.

PS >$var1 = "Hello

PS >$var2 = "world"

  1. To use the plus (+) operator to concatenatethe two string variables, type the following command, and thenpress ENTER.

PS >$var1 + $var2 The result of thisoperation is a new Hello World string.

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

  1. You can use the .NET String properties to inspect the stringobjects. To use the Length property to obtain the size incharacters of the previous string concatenation, type the followingcommand, and then press ENTER.

PS >($var1 + $var2).Length

4. Windows PowerShell also provides other kinds of binaryoperators, like comparison operators. To verify if two strings areequal, 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 thanor equal) and –le (less than or equals).

  1. Formatting is a common task that can also be done in WindowsPowerShell. To use a custom format to display 12.4 as 12.40, typethe following command, and then press ENTER.

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

  1. To display the same number as currency, and then to pad it to10 characters aligned to the right, type the following command, andthen 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 thelocal machine is used.

7. Date and time formatting can also be done. To display onlyhours and minutes from the current date, type the followingcommand, 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 afile, providing an easy way to run a list of commands. You onlyneed 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 WindowsPowerShell, you will be introduced to a Windows PowerShell securityfeature called execution policies. The execution policy enables youto determine which Windows PowerShell scripts (if any) will beallowed to run on your computer. Windows PowerShell has fourdifferent execution policies:

AllSigned– Only scripts signed by a trustedpublisher can be run.

RemoteSigned– Downloaded scripts must be signedby a trusted publisher before they can be run.

When you first install Windows PowerShell, the default value forthe execution policy will be set to Restricted.

1. To display the current execution policy, type the followingcommand, and then press ENTER.

PS >Get-ExecutionPolicy

2. Before running a script file, you will have to change theexecution policy. To change the execution policy to RemoteSignedand verify the change, type the following commands, pressing ENTERafter each one.

PS >Set-ExecutionPolicy Unrestricted

PS > Get-ExecutionPolicy Changing theexecution policy requires administrative privileges.

  1. To create the script file, on the taskbar, right-click theWindows PowerShell icon, and then click Windows PowerShellISE.

To create script files, you don’t need a special editor. In thisexample you will use the Windows PowerShell Integrated ScriptingEnvironment (ISE). The Windows PowerShell ISE is a host applicationthat enables you to run commands, write, test, and debug scripts ina friendly, syntax-colored, Unicode-compliantenvironment.

  1. On the View menu, click Show ScriptPane.

The ISE has two windows, or panes, so you do not need to edittext in a separate application.

in the Windows PowerShell text-based console.

  1. In the script pane, type the following commands, pressing ENTERafter each line.

# test.ps1

# Show Hello and time.

"" # Blank Line

"Hello " + $env:UserName + "!"

"Time is " + "{0:HH}:{0:mm}" -f (Get-Date)

"" # Blank Line

  1. To save the file as a Windows PowerShell script file, on theFile menu, click Save As.
  2. When you create your script file, the filename must have a .ps1extension.

  1. Click in the command pane, type the following command, and thenpress ENTER.

To execute the script file you created in the previous step,type the following command, and then press ENTER. >.test.ps1

You can follow the same procedure to execute scripts in theWindows PowerShell text-based console. From the ISE, you can alsoexecute a script using the Run command on the File menu.

Preceding the script name with directory information, in thiscase the current directory (.), instructs WindowsPowerShell to run a script.

There must be no space between . and thescript name. Adding the .ps1 extension is optional. You mustspecify the path to the script file, even if the script is in thecurrent directory.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!