Explore basic PowerShell operations

Source: Internet
Author: User
Tags perl script

PowerShell Console

Open PowerShell and the following page is displayed:

Command Prompt Prefix:

PS C:\Users\MaRui>

PS means running PowerShell, and C: Is my main directory drive number, different machines will be different.

The most basic operations are the same as those of cmd, dos, and sh.

Cmdlet command

Although more than one hundred new PowerShell commands need to be learned, PowerShell commands have been standardized like most command line interfaces. It uses the name of "verb-noun" as a cmdlet command. This standard greatly reduces the learning difficulty and provides better descriptive help for all cmdlet commands.

To view all the cmdlets in PowerShell, run the following command:

get-command <enter>

Next, let's use a specific verb to list all the commands. The following is the cmdlet filtered by the verb "Get:

get-command -Verb Get <enter>

Other commands can also use this filtering method. After this combination, hundreds of commands will be well remembered.

Simple examples of other basic common commands:

get-help <enter>

get-help * <enter>

get-service <enter>

get-help get-service <enter>

Try the following buttons to see what will happen:

Page Up-jump to the first command in the History buffer.

Page Down-jump to the last command in the History buffer.

UpArrow-display the history buffer command backward.

DownArrow-display history buffer commands to the forward.

Home-jump to the beginning of the command line.

End-jump to the End of the command line.

Ctrl + LeftArrow-to the leftmost character.

Ctrl + RightArrow-to the end of the right word.

Tab-complete the input (in the console, type GET-C and press the TAB key, and then press Tab to try ).

F7 key-display history buffer (use the up and down arrow keys to browse the buffer ).

You can also copy a text in another window and right-click PowerShell to try it, similar to the role of the middle mouse button under sh.

In PowerShell commands, another class is called "Native window commands ". For example, you can start the GUI window of services. msc from the PowerShell command line.

services.msc <enter>

Since PowerShell is used, we can ignore services. msc.

The following is a command to operate the service <take the Brower service as an example>:

get-service -name Browser <enter>
stop-service -name Browser <enter>
get-service -name Browser <enter>

Note that when you use the cmdlet to stop the service, there is no indication that the service has been successfully stopped.

Similarly, start the service command:

start-service -name Browser <enter>

After the service is started, there will be no feedback indicating that the service is started normally, unless the service fails to be started.

This is a simple introduction to the command, isn't it? You only need to remember the form of "verb-noun!

The following is an example of the basic operation syntax in PowerShell.

PS C:\> 99 + 100
199

In PowerShell, you can perform mathematical operations very easily. All you need to do is to input an expression so easily that the results will be automatically output, in this process, you do not need to use the print statements in other shells. The execution results obtained after the calculation are not discarded at will, but are directly output, later we will learn how to discard the results.

PS C:\> "Hello World!"
Hello World!

"Hello World", just like the mathematical calculation above, simply enter a string. If you have used a Perl script, you will feel familiar with it, because PowerShell draws on the advantages of Perl to a large extent.

PS C:\> (7 - 2 * 3) / 5
0.2

PowerShell also supports four mixed operations. You can use () in an expression to adjust the priority and associativity of operators. PowerShell supports several different data types, such as int, float, and double. In the future, we will be exposed to type conversion and other content.

PS C:\> (2+2)*3/7 > c:\fun.txt
PS C:\> type c:\fun.txt
1.71428571428571

Similarly, in addition to screen output, we can store results to temporary files, and then use the type command to retrieve the results of the files.

PS C:\> $n = (2+2)*3
PS C:\> $n
12
PS C:\> $n / 7
1.71428571428571

In addition, we can also store the calculation results to the variables by assigning values to the variables, and use the variables for subsequent calculations.

PS C:\> $files = dir
PS C:\> $files[3]

PS C:\> $files = dir
PS C:\> $files[3]
Directory: Microsoft.PowerShell.Core\FileSystem::C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d-r-- 2010-3-26 21:25 Program Files

As an object-oriented language, we can simply assign the result returned by the command to the variable. $ Files contains the object set of the Directory items under the current directory. You can obtain the objects at this location using the regular array access syntax. The Program Files directory is displayed in the example. Note: The array subscript in PowerShell starts from 0, which is exactly the same as. Net Common Language Runtime.

This section is here. If you are interested, you can continue to pay attention to the next section.

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.