Ten Basic Concepts you must know about PowerShell scripts

Source: Internet
Author: User

BKJIA featured translations] PowerShell scripts provide a convenient way to automate various things. The following are some basic concepts about PowerShell. For beginners of PowerShell, mastering these concepts will help them deepen their understanding of PowerShell scripts.

Recommended topics for BKJIA: script technology in Windows-Windows Powershell

1. PS1 File

A PowerShell script is actually a simple text file, which contains a series of PowerShell commands. Each Command is displayed as an independent line. For a text file that is considered as a PowerShell script, the file name must be used. PS1 extension.

2. Execution permission

To prevent execution of malicious scripts, PowerShell has an execution policy. By default, this execution policy is set to Restricted. This means that PowerShell scripts cannot be executed, you can use the following cmdlet command to determine the current execution policy:

Get-ExecutionPolicy

You can choose to use the following execution policies:

You can use the following cmdlet command to set the PowerShell execution policy:

Set-ExecutionPolicy <policy name>

3. Run the script

If you want to run an executable file from the command line, a permanent way for many years is to go to the location where the executable file is located in the command line, and then type the name of the executable file, however, this old method does not apply to PowerShell executable scripts.

To execute a PowerShell SCRIPT, you must enter the complete path and file name. For example, if you want to run a SCRIPT named SCRIPT. PS1, you can enter:

C:\Scripts\Script.ps1

The biggest exception is that if the PowerShell script file is located in your system directory, you can directly run it by typing the script file name at the command prompt, for example:

.\Script.ps1

Note that. \ must be added before, which is the same as the method for executing Shell scripts in Linux.

4. Pipelines

The role of the pipeline is to use the output of one command as the input of another command. You only need to use the pipeline symbol |) to connect the two commands or cmdlets.

To help you understand how the pipeline works, let's take an example. Suppose you want to create a list of processes running on the server and sort them by the process ID, you can use the Get-Process cmdlet command to obtain the Process list, but the list is not sorted by default. If you use a pipe to output the command, the process list is sorted by the process ID, for example:

Get-Process | Sort-Object ID

5. Variables

Although the pipeline can be used to deliver the output of one command to another, the pipeline itself is also limited. When you use the pipeline to pass the output result from one command to another, the output results are immediately used, but sometimes you may need to save the output results for a while so that they can be used or reused later). At this time, the pipeline should end and the variable will be used.

It is easy to think of variables as a repository, but in PowerShell, variables can save the complete output of commands. For example, if you want to save the list of processes in the running state of the server, you can assign it to a variable, such:

$a = Get-Process

Here, the variable is named $ a. If you want to use this variable, you only need to simply call its name. For example, type $ a to print the variable content on the screen.

You can assign the final output of multiple commands connected with pipelines to a variable. You only need to enclose the command with a pair of parentheses. For example, if you want to sort running processes by process ID and output the results to a variable, you can use the following command:

$a = (Get-Process | Sort-Object ID)

6. @ symbol

By using the @ symbol, you can convert the list content into an array. For example, the following code creates a variable named $ Procs, which contains an array of multi-line text content ):

$procs = @{name="explorer","svchost"}

You can also use the @ symbol when using a variable. To ensure that it is processed as an array rather than a single value, for example, the following code will run the Get-Process cmdlet command on the variable I defined earlier:

Get-Process @procs

Windows displays all processes used by Windows Resource Manager and Svchost. Pay attention to the @ symbol used before the variable, rather than the common $ symbol.

7. Split

The Split operator Splits a text string based on the characters you specify. For example, if you want to Split a sentence into an array composed of words, you can use the following command:

"This is a test" -split " "

The split result is as follows:

This is a test

8. Join

Just as Split can Split a text string into multiple blocks, the Join operation is reverse, and multiple independent blocks are connected into a whole. For example, the following code creates a text string consisting of my first name and last name:

"Brien","Posey" -join " "

The space between double quotation marks at the end of the Command tells Windows to insert a space between two text strings.

9. breakpoint

When running a newly created PowerShell script, if the script has a Bug, unexpected consequences will occur. One way to protect yourself is to insert a breakpoint in the key position of the script, in this way, you can ensure that the script runs normally first and then handle possible problems.

The simplest way to insert a breakpoint is to insert a breakpoint Based on the row number. For example, if you want to insert a breakpoint in row 10th, you can use the following command:

New-PSBreakpoint -Script C:\Scripts\Script.ps1 -Line 10

You can also bind a breakpoint to a variable. If you want your script to modify the content of a $ at any time, run the following command:

New-PSBreakpoint -Script C:\scripts\Script.ps1 -variables a

Note that I did not include the dollar sign after the variable name.

Verbs that can be used with PSBreakpoint include New, Get, Enable, Disable, and Remove.

10. Step

When debugging a script, you may need to run the script line by line. In this case, you can use the Step-Into cmdlet command to execute the script line by line, whether or not the breakpoint is set, if you want to exit from this Step-by-Step running mode, use the Step-Out cmdlet command, but note that the breakpoint is still valid after you use the Step-Out cmdlet command.

By the way, if your script uses a function, you may be more interested in the Step-Out cmdlet. The Step-Out method is the same as Step-Into. However, if a function is called, Windows will not execute it step by step, and the entire function will be executed at one time.

For the bkjia.com Translation, please indicate the translator and source of the original article .]

Original article: 10 fundamental concepts for PowerShell scripting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.