Easy to learn and use Windows powershell

Source: Internet
Author: User

Windows powershell is a shell and script language technology developed by Microsoft for the Windows environment. This new technology provides rich control and automated system management capabilities; scripting languages are used to compileProgramComputer language. The scripting language is usually simple, easy to learn, and easy to use. It is designed to allow developers to quickly write programs. This article briefly describes the origin and purpose of the script language and introduces windows powershell.
Script language source and purpose

The scripting language is a kind of computer programming language (programming languages). Therefore, it can also allow developers to write programs that allow computers to listen to their lives. However, "quickly accomplish certain complex things in a simple way" is usually an important principle for creating a scripting language. Based on this principle, the scripting language is generally simpler and easier than system programming languages such as C, C ++, or Java 」, also, let the scripting language have some other so-called "Features of The scripting language 」:•

Syntax and structure are usually relatively simple •

It is usually relatively simple to learn and use •

Generally, the program is executed in a "literal translation" that is easy to modify without compilation •

Program development capacity is superior to execution efficiency

The scripting language originated from the command column computer system in the early text mode. When the computer can only be operated by typing commands on the keyboard at that time, in order to reduce the number of times that commands need to be repeatedly typed, the command processor of computer operating systems (usually called command shell in the Unix world) provides the batch operation method, allows the computer operator to orchestrate the entire flow of commands in a text file called batch file (also known as batch jobs or shell scripts) in a sequential and line-by-line manner, if the same operation needs to be processed, the batch file can be loaded without repeated instructions.

Shell script

Because batch processing is of great help to the operator of the computer system in the Command column, the command processor of the operating system is also constantly strengthening its functions, especially the important elements of many programming languages, including variables, loops, and condition judgments, so that computer operations can more accurately control the operation of computer systems than just sequential execution of commands. Such a program is usually called shell programming. Therefore, whether it is a system operator of a large host, or a system administrator or network administrator of the entire computer environment, shell programming is a very important skill that can effectively and quickly manage its computers.

The shell of UNIX systems is quite diverse, such as Bourne shell (SH), Bourne-again shell (BASH), KoRn shell (Ksh), and C shell (CSH; the following describes the common unixshell. •

Bourne shell (SH): this is a historical and important Unix shell, created by Stephen Bourne, a researcher at Bell's lab, for the seventh version of Unix. The execution file extension of this shell is. Sh. •

Bourne-again shell (BASH): the light can tell from the name that Bash and sh are related (or even "kinship 」). Bash was created by Brian fox in 1987 for GNU. Since 1990, Chet Ramey became the main maintainer. Bash is a very popular shell. Apart from the shell preset in most Linux systems, bash also has traces in many UNIX-like systems (such as Mac OS X Tiger ), it can even be executed in windows through cygwin.

Korn shell (Ksh): created by David Korn, a researcher at Bell Labs, in the early 1980s S. ksh is fully compatible with SH and contains several C Shell functions.

C shell (CSH): it was created by Bill Joy (later founded Sun) for the bsd unix system. CSH originated from the sixth unix version of the Thompson shell (the predecessor of the Bourne shell), because the syntax is similar to the C language.

In addition to the preceding shell that prevails in the Unix environment, other operating systems also have exclusive shell or shell scripting languages, for example, Apple Mac applescript and ibm OS/2 Rexx (restructured extended executor ). While DOS and early Windows shell are command.com, but their functions are also weak. Therefore, they can be enhanced by software such as 4dos and 4nt, in Windows 2000 and later versions, cmd.exe is used as the Windows Shell with strong functions, and the Windows Script Host is used as the script language execution environment, which greatly improves the availability of Windows Shell. Microsoft will also launch a more powerful Windows Shell, known as windows powershell.

Although many computer systems now provide graphical operations, they do not stop providing command columns in the text mode. On the contrary, many systems have enhanced these functions. For example, Windows not only enhances the function and quantity of operating commands, but also improves the shell programming method. However, the reason for strengthening and improving is, of course, not good enough. Windows's graphical operations on a single client computer are quite convenient, but if it is a group of client computers, or a 24-hour server computer, graphical operations are sometimes inconvenient.

Macro language

Macro language can also be considered as a branch of the scripting language. The two are essentially the same. Macro language is usually attached to a software. For example, Microsoft Office, Visual Studio, and Adobe Photoshop all have built-in macro language functions. You can use the macro language attached to the software, achieve automated results (such as adjusting the resolution of all PSD image files in a data clip ). Most of these macro languages also provide the recording macro function to automatically record users' keyboard keys so that users can generate macro programs without having to write programs. In addition, the C language and assembly language also have macro functions, which are used to search for and replace the pre-processing before compilation or group translation.

Windows powershellBirth

In the Unix world, there is a powerful shell, and now there is a Windows environment; Windows powershell not only provides a function equivalent to the bash command column shell, but also built-in scripting language and auxiliary scripting tools. Windows powershell is Based on. NET technology and is compatible with existing wsh backtracing. Therefore, the script program of Windows powershell can not only access. Net CLR, but also use the existing COM technology.

Windows powershell contains several system management tools and simple and consistent syntaxes to improve the management of common processes such as logging on to the database and WMI. Server software such as Exchange Server 2007 and System Center Operations Manager 2007 will be built into windows powershell.

Monad is the project code before Windows powershell and Microsoft shell (MSH) is another alias. In short, Windows powershell has the following features:

Consistent design allows all tools and system data to use the same syntax and naming principles.

The scripting language of Windows powershell is not only easy to learn, but also supports existing script programs and command column tools. •

There are 129 standard tools called cmdlets that can be used to handle common system management tasks, such as logging on to a database, system services, formation, WMI, and Event Logs. •

With complete expansion functions, independent software vendors or developers can easily expand on their own.

Download, install, and executeWindows powershell

Windows powershell can be installed on Windows XP and later Windows clients or servers, including :•

Client: Windows XP, Windows XP Service Pack 1, Windows XP Service Pack 2, Windows Vista •

Server: Windows Server 2003, Windows Server 2003 R2, and Windows Server 2003 Service Pack 1

In addition to the 32-bit version, Windows powershell also has x64 and IA64 64 64-bit versions, which can be installed on these two 64-bit Windows platforms. In addition, because Windows powershell requires. NET Framework 2.0, install. NET Framework 2.0 on your computer first.

After installation, you can run Windows powershell in the following three ways:

The "Windows powershell" shortcut in "Start/All Programs.

In the "Start/Run" command window, enter powershell.

In any command prompt character window, enter the command prompt symbol powershell.

The Windows powershell window looks similar to the original "command prompt character" window, and I usually adjust the size of the window and font to facilitate the job.

Preliminary ExplorationWindows powershell

Any shell has commands for listing directories and files. What about Windows powershell? Enter the DOS dir first!


Figure 1 dir command execution result

Well! DIR is also available, but the list method is different from the previous dir command. On the Rise, I entered the Unix LS command, and the window showed the same result as the Dir command. It was really interesting (at this time I didn't read any windows powershell file, but it was just intuitive to enter the command ).

Windows powershellCommand

The cmdlet is a Windows powershell command, which is equivalent to a built-in command of DoS or other shell (but the entered DIR or LS is not a cmdlet, which will be explained later ), the command-Let command is used to pronounce a cmdlet. The name format of a cmdlet is a pair of verbs and nouns separated by a hyphen, and is generally a singular noun. For example, the cmdlet that can be used to query the cmdlet online is get-help. The verbs of the cmdlet name include get, set, add, remove, and so on (Windows powershell has the same operations as DOS, and the input letters are case-insensitive ).

Get-help is an important cmdlet that allows you to query all windows powershell commands and instructions online. For example :•

Get-help *: lists all topics, including commands and concepts.

Get-help * | more: lists all topics, including commands and concepts, and pauses when the window is full.

Get-help about *: lists all concepts and themes, such as tens of thousands of characters and foreach loops.

Get-help get *: lists all topics starting with get. •

Get-help {<command name or topic Name>}: Describes the specified command or topic. For example, get-help dir can be used to query the Dir command, get-help about_wildcard can query the usage of the topic "characters.

In addition, you can also replace get-help with help. The biggest advantage is that the help displays the entire window, And the preset will automatically pause.

Dir and LS were used at the beginning. Although this is not a cmdlet, it can indeed be used in Windows powershell. In the past, such commands were called alias (not a real name), similar to the cmdlet shortcut. For example, Dir and LS are actually alias of get-childitem, and get-childitem is the cmdlet; the get-childitem function lists the sub-projects at the specified position of the disk drive.

Windows powershell Commands include many built-in alias, which are familiar with DoS or shell commands except the Dir and LS commands mentioned above, for example, the RM, rmdir, RD, CLS, and copy commands of the original DOS commands, as well as the cat, sleep, clear, and CP commands of bash are all alias of Windows powershell, that is, you can directly use these alias commands. to list all alias, you can use the get-alias command. In addition to the built-in alias, Windows powershell also allows users to establish their own alias with set-alias.

In addition to the cmdlet and alias, Windows powershell commands also have two types: Application and function. The former refers to independent program files (called external commands in the DOS era ), the latter is a program with a nameCodeBlock, that is, a function: can be used as a Windows powershell command or a script program, regardless of the application or function. You can use get-command * to list all commands.

Windows powershellPipeline Function

Both dos and Unix shell can use the pipeline symbol to transfer the execution result of the command to another command, so that the other command can process the data transferred from the previous command. The pipeline symbol of Windows powershell is also |, for example, get-help * | more, which transfers the result of get-help * to more, more will suspend the entire window when the result is full.

Pipelines are the best example of "Adhesion" in various shell environments. For example, pipelines are attached to various commands of Windows powershell, so that each command can complete a task. Here are some examples of pipelines. First:

Ipconfig | findstr "Address"

The ipconfig and findstr that are pasted by the pipeline are both external commands of Win32. The former displays and sets the IP address of the computer, and the latter can find a specific string in various ways. The preceding description transfers the execution result of ipconfig to findstr and asks findstr to find the address string of the execution result.

Figure 2 ipconfig | findstr "Address" execution result

Here are two more examples. The following describes stopped system services, but get-service and where-object work together to list all system services, the latter filters out system services in the stopped status:

Get-service | where-object {$ _. Status-EQ "STOPPED "}

However, the results described above are arranged by the System Service name. If you want to sort the system service by the "display name" in sequence, you can add sort-object, and it is also pasted with pipelines:

Get-service | where-object {$ _. Status-EQ "STOPPED"} | sort-object-property displayname


Figure 3 execution results of the Tri-pipeline sample

New scripting language

Microsoft has created a new scripting language for Windows powershell, but if you are familiar with any scripting language, you will soon be familiar with the scripting language for Windows powershell. Basically, you only need to put windows powershell commands into a text file with the extension of. PS1. This file is the script program of Windows powershell. Therefore, after being familiar with the command operations of Windows powershell, as long as you are familiar with the variables, arrays, loop control, and condition control of Windows powershell script language, the language is almost the same. However, to use a script program to manage Windows systems, in addition to the scripting language, you must be familiar with Windows powershell commands and WMI. After all, the purpose of the script language is to combine these ready-made software components.

There are several precautions for executing the script program. First, you must determine that the script program is located in the execution path. In addition, due to Windows powershell's security concerns, the preset is to prohibit computer systems from executing any script programs. to unban the programs, you must use the set-executionpolicy command. This command has three parameters:

Set-executionpolicy allsigned: allows execution of script programs with trusted signatures. •

Set-executionpolicy remotesigned: Only scripts with trusted signatures can be downloaded remotely. •

Set-executionpolicy unrestricted: This is the loose condition. It allows execution of any script program, regardless of whether or not you trust the signature. However, the script programs you write do not usually trust the signatures.

Use Scripting

From the development trend of programming languages in the past decade, we can see that the development of scripting languages is more diverse and more active than that of system programming languages, and developers have a high degree of acceptance. Of course, each of these two types of programming languages has its own applicability. No one replaces anyone, but how to choose an appropriate method to complete the work. John K. ousterhout (the creator of Tcl/tk) once put forward his views in scripting: higher level programming for the 21st century. He believes that the main purpose of the scripting language is, because of the rise of graphic operation interfaces, the Internet, and component application frameworks (component frameworks, more and better scripting languages are required for bonding (therefore, the scripting language is also called glue languages) or system integration languages ).

Of course, the improvement of script language capabilities and the speed of computer hardware are also the reason for the vigorous development of script language. Because most scripting languages use literal translation, execution efficiency is slow when Billy uses the compiled system program language. However, when the computer hardware speed is getting faster and faster, the user gradually cannot feel the speed of the script language is slow.

Simple, easy-to-learn, and easy-to-use scripting languages are especially suitable for IT staff dedicated to system or network management, because the scripting language originated to facilitate system operations, in addition, suppliers of operating systems will also strengthen this part of the function, provide more simple syntax, more rich components, and make it easier for IT staff to "integrate" the functions, and combine the script programs that are absolutely suitable for your own needs.

Make good useWindows powershell

Windows powershell re-closely binds the separate scripting language (wsh) and Operation commands in the Windows environment, and enables Windows to have a shell environment and script language equivalent to the Unix environment, windows is more suitable for large, complex, and important computer computing environments.

Of course, the features and advantages of Windows powershell are not only described in this article, but as long as you follow this article, you will be able to download, install, and try windows powershell functions in person, in particular, Windows powershell regards database login as a disk drive for access, which is very appropriate and simple, because the structure of the host code and host code value is like a directory and a file. When downloading and installing Windows powershell, do not forget to download the latest official windows powershell files.

Http://www.microsoft.com/taiwan/technet/columns/profwin/28-monad.mspx

Reference data and network resources

Download Windows powershell:
Http://www.microsoft.com/technet/scriptcenter/topics/msh/download.mspx •

Technet windows powershell Script Center:
Http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Msdn windows powershell blog:
Http://blogs.msdn.com/powershell/

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.