Powershell _ basic self-study course _ 1 _ first knowledge of powershell

Source: Internet
Author: User

Welcome to reprint this seriesArticleFor more information, see www.cnblogs.com/volcanol.

Since Microsoft launched. net, Microsoft's Windows system has undergone great changes. First, it has changed the interface of the operating system, such as the interface changes of Vista and XP,

Secondly, there are some application changes. For example, there is no aero desktop effect in XP, but there is aero desktop effect in win7 (I have never used Vista, I don't know if there is any aero effect ).

But these are users' intuitive feelings. In addition to these, there are also deeper changes, such as C ++ Clr and CLI, and some very popular technologies (such as WPF and WCF ).

Workflow and so on. I don't understand this, but I have heard of this.) Here is another change other than these changes: the change of the console shell.

The main scripts in XP are VBScript, JavaScript, and batchfile batch processing. These functions are powerful and often have some unexpected results. For example, before

Remove the one obtained in two daysCodeBatch Processing of leading numbers. After Vista, Microsoft developed a brand new console shell-powershell for Windows.

I. Functions of powershell

1. With standard Windows commands and ApplicationsProgramCompatible

2. New Command Format: cmdlets

3. Supported objects/object-oriented

4. Suitable for administrator applications

5. multi-language support

6. Discovery functions: Get-command, get-help, and get-member commands

Ii. Start powershell

1. CMD ---> powershell

2. Start --> All program --> attachment --> Windows powershell

After the startup, the interface is shown as follows:

 

3. powershell Basics

3. 1. How to execute programs/commands

1) directly enter the command or program name to execute the program or command

Exp:

Program.exe arguments

Scriptname. PSL arguments

Batchfilename. BAT/CMD arguments

2) to run a command that contains spaces, enclose the command in single quotes and add the "&" symbol before the command. This is called a call operation in powershell.

(Invoke operator)

Exp:

& 'C: \ Program Files \ Program \ program.exe 'arguments

3) run commands or programs in the current directory or path.

In this regard, powershell draws on the Linux/Unix experience. When running a program or command in a non-system path in powershell, you must specify the command explicitly.

Or program path.

Run the command or program in the current directory or path by adding ". \" before the command.

Exp:

. \ Program.exe arguments

4) run the program or command with spaces in the current directory or path. The command or program name should be executed in combination with rule 2) and Rule 3.

Exp:

& '. \ Show System infomation. Pls'

 

5) Special Points in powershell

We know that at the CMD prompt, double quotation marks are used to access the command with spaces or the command in the path. In powershell, double quotation marks have special purposes:

String.

Exp:

If we enter "hello" + "world" at the ps command prompt, the output is:

Hello World

That is, double quotation marks in PS follow the C language specification.

 

6) You can run the built-in cmdlets of powershell in any path without adding special control symbols such as. \ or.

Exp:

Get-Process

Enter the preceding command to implement the tasklist function in cmd.

 

3. 2 cmdlet

The built-in commands in powershell are called cmdlets. The cmdlet implementation has the following features:

1) Unified Command form

2) support for MPs queue

3) Output objects that are easy to manage and support object-oriented concepts

3. 3. Customize powershell prompts and command aliases

The appearance of powershell makes up for the console shell function inherent in windows, and uses many bash shells or other Linux/Unix shells for reference in powershell.

User-Defined command alias is one of them.

In powershell, there is a built-in variable $ profile, which indicates the powershell User-Defined configuration file. You can enter $ profile in powershell.

To view the content of this variable:

PS c: \ Users \ vol_20120330> $ profile c: \ Users \ vol_20120330 \ Documents \ windowspowershell \ microsoft. powershell_profile. PS1 PS c: \ Users \ vol_20120330>

Run the following command to create a script file for the user configuration file:

 
New-item-type file Force $ Profile

The user's custom powershell environment configuration file is referenced through the $ profile file. You can run the following command to open and edit the configuration file:

 
Notepad $ Profile

Run the following command to obtain the configuration file: $ Profile

Exp:

 
Get-item $ Profile

The get-item command gets the general information of the configuration file. You can also use the following command to obtain $ profile information:

 
Get-childitem $ Profile

Next we use an instance to modify the command prompt form of powershell. First, open the user configuration script file through notepade $ profile, and then enter the following information:

Function prompt {"PS [$ ENV: computername]> volcanol> "}

In this way, our command prompt will become the following style:

 
PS [vol_20120305] volcanol>"

Note: My computer name is vol_20120305

3. 4. powershell Security Execution Policy

By default, powershell does not allow execution of script files. The preceding user configuration files are also included. If you do not modify the powershell Security Execution policy

The configuration file cannot be executed. This is not illustrated here. you can experiment on your own.

The following describes how to modify the powershell Security Execution policy.

1) Open the powershell Prompt window as an administrator

2) enter the following command:

 
Set-executionpolicy remotesigned

3) Start powershell again as the administrator. Then you can see that the PS prompt is programmed in a custom form.

Or you can run the $ profile user configuration script once instead of exiting the PS.

 

3. 5. Custom command alias

You can customize the command alias in the user configuration script file to improve the script editing efficiency, because PS commands are much longer than cmd commands, such

The CD command is now replaced by the Set-location command; the Dir command is replaced by the get-childitem command.

Use the: set-alias command to set the command alias.

 
Set-alias New-objector

Exp: Set the alias of the IE browser startup command.

 
Set-alias iexplorer 'C: \ Program Files \ Internet Explorer \ icycler.exe'

In this way, you can use the iexplorer command in powershell to start IE. Of course, this requires restarting powershell or executing a User Configuration File Script.

 

3. 6 search commands

When we use cmd, we have such experience. Sometimes we don't know what command to use to implement the required functions. Then we can use the help command to perform a general search;

Then check whether there are any commands we need. This process is also supported in powershell. We can search for commands, which are implemented through get-command.

1) To obtain all the available commands, enter the following command

 
Get-command

2) Get the help information of the specified command

Get-command command_name

Example: Get-Command get-Process

3) search with wildcards

Powershell supports wildcard search, which is perfectly supported and comparable to regular expressions in Linux.

 
Get-command * char *

When only * is used for search, it is equivalent to: Get-command.

 
Get-command *

For example, the command with txt in the search Command: Get-command * TXT *

5) search with Parameters

When you use get-command in powershell to search, you can include the option parameter,-verb, and-verb. The-verb option is used to search for commands with the specified verb, and the-verb option is used to search for commands with the specified verb.

To search for commands with specified terms. The command format is as follows:

 
Get-command-verb verb_nameget-command-Noun noun_name

For example, search for the GET command: Get-command-verb get

Search for a command with a noun service: Get-command-Service

 

3.7 cmdlets -- consistent command interface mode

Powershell adopts a command interface mode called cmdlets. All commands follow this command mode:

 
Verb-term

For example, for the get-command, get is a verb, while command is a noun.

Get-process command, get is a verb, and process is a noun

Get-Eventlog command. Get is a verb, while EventLog is a noun.

In eaglets, the verb part of a command is taken from a set of formulated verbs, and the noun part of the cmdlet command describes the object to be operated by the command.

 

3.8 get help information

There are three ways to get help in cmd: a) use the help command, such as help CD

B) Use /? Command options, such as CD /?

C) view system help group information

There are four methods in powershell:

A) use the get-command

B) Use the get-HELP command

C) use -? Command Options

D) view system help information

The use of the get-command and the use of the get-HELP command to obtain command help information is different; get-command directly from the cmdlet, function, variable

The get-HELP command obtains information from the system help topic file. Generally, the get-HELP command obtains more help information than the get-command.

Command details.

1) Use the get-command to obtain help information:

Exp: Get-Help help information using get-command

 
Get-Command get-help

If you need to use the get-command to obtain more detailed commands, you need to use the pipeline and format-LIST commands; as shown below:

 
Get-command command_name | format-list

2) use the get-HELP command to obtain help information:

Exp: Get your help information using get-help

 
Get-help get-help

You can use the get-HELP command to obtain help information by specifying Command Options to obtain help group information of different levels:

A)-detailed option; get the help information of a specific command; for example: Get-help-detailed get-Process

B)-full option; obtain all the help information of a specific command. For example: Get-help-Full Set-location

C)-examples option; get the instance help information of a specific command; for example: Get-help-examples get-childitem

 

 

The above is all the content of this time, to be continued.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.