Return to the pleasure of DOS operations and enter the powershell world

Source: Internet
Author: User

Write down this articleArticleThis was due to attending a tech. Ed course on window powershell. Although I have been paying attention to it before, I have never personally experienced it. Windows powershell is a new command line shell and script language used for system management and automated tasks. IT professionals and developers can control and automatically complete windows and applications.Program. What's more powerful is that it can call class libraries in. NET Framework and interact with COM, ADO, XML, and so on. In this article, let's take a look at the pleasure of returning to the doc operation and enter the powerful windows powershell world.

Windows powershell includes:

1. 129 command line tools (called "cmdlets") are used to execute common system management tasks, such as managing services, processes, Event Logs, certificates, registries, and using Windows Management Instrumentation (Wmi ).

2. a command line tool using standard naming conventions and common parameters is easy to master and use. It is also a simple tool for sorting, filtering, and format setting data and objects.

3. Support for existing scripting languages, existing command line tools, and a variety of Windows versions, including Windows XP, Windows Server 2003, Windows Vista, and Windows Server 2008.

4. convenient features enable users to browse data storage (such as registry and certificate storage), just like facing file systems.

5. standard utilities used to manage Windows data in different storage and in different formats, including Active Directory (ADSI), Windows Management Instrumentation (Wmi), Component Object Model (COM) object, ActiveX Data Object (ADO), HTML, and XML.

6. Advanced Expression Analysis and. Net object processing on the command line, including pipeline processing of objects to help improve the efficiency of IT professionals.

7. scalable interfaces enable independent software vendors and enterprise developers to build custom cmdlet to meet unique application and system management requirements.

Simple Example

Before starting, let's take a look at a simple example to get a section of all processes in the current system in. net.Code:

Static voidMain (String[] ARGs ){Process[] Proceses =Process. Getprocesses ();Foreach(VaRItemInProceses ){Console. Writeline (item. processname );}Console. Readline ();}

A simple piece of code directly Retrieves all processes and displays their names, as shown in:

After installing Windows powershell, you can see that its interface is very similar to the common command line interface. Now we can use Windows powershell to retrieve all the processes in the current system. Only one command can be completed:


Shows the output:


Powershell command

In Windows powershell, We have 129 built-in commands for use. These commands are called "cmdlets ". In addition to a lot of DOS commands, there are also some built-in powershell commands. Each Command is composed of two parts. The first half represents the action, and the second half represents the subject of the action, such as get-help, get-command, and write-host. When you enter a command in Windows powershell, you do not need to enter all the names. You can enter only a few keywords. powershell will automatically match the command. For example, you can only enter get-he and press the "tab" key, the complete get-HELP command is displayed on the screen.

Get-help and get-command are two important commands in powershell. They can get help and get a list of all commands, such as entering:


The result is shown in. All commands are listed:

Interaction with. NET Framework

Windows powershell is more powerful because it can be accessed. net Framework class library, we can use the new-object command to create.. NET Framework class instance and save it in the variable. The command is as follows:

$ D = new-object-typename system. datetime

Here we use new-object to create a datetime object, and use-typename to specify the type name. The created object is saved in the $ D variable. The output result is shown in:

You can also use-argumentlist to specify parameters, as shown in:


Interaction with COM

Windows powershell can also fully interact with COM, which makes windows powershell more powerful. net Framework interaction, coupled with COM interaction, can be said that Windows powershell is almost omnipotent. For example, if you use powershell to access IE, you can use the following command to create an IE Object and navigate it to the blog homepage:

$ Ie = new-object-comobject internetexplorer. Application $ ie. Visible = $True$ Ie. navigate (Http://") $ Ie. Quit ()

To briefly explain this code, first create an ie com Object and display it. Then, navigate to the blog homepage and exit the program. As shown in:


In this process, you will see that the system opens an IE window and navigate to the blog garden.

Access the Registry

In addition, Windows powershell can fully control the Registry, including querying all the entries, or modifying or deleting key values. It can be said that everything is controllable, use the following command:

Get-childitem-path hkcu :\

As shown in the output, information under HKEY_CURRENT_USER is listed:

Create an alias

In Windows powershell, you can also create aliases that you are familiar with for commands, so that some too long command names can be expressed by your abbreviations, or for some commands that are not easy to remember, use your favorite name.

Set-alias GH get-help

Set GH as the alias of the get-HELP command. In the subsequent operation, after GH is entered, the name of the command is get-help, as shown in:


Output file

In Windows powershell, we can also output the operation results to a file, use the out-file command, and use-filepath to specify the output path and file name, the following command is used:

Get-process | out-file-filepath c: \ temp.txt

Open the temp.txt file of the C drive. The output result is as follows:

Handles NPM (k) PM (k) WS (k) VM (m) CPU (s) id processname ------- ----- ------ -- ----------- 126 4 13272 10220 47 1204 audiodg 73 4 2936 8080 61 1.05 3580 conime 512 5 2188 4112 2.06 532 CSRSS 511 7 5704 19744 182 93.44 588 CSRSS 1249 109 127144 157128 561 44.67 6648 devenv 188 8 47088 73332 164 2,020.61 DWM 2508 48 2261 75040 72600 416 341.38 explorer 0 0 0 24 0 idle 2548 28 664 36696 49076 173 14.78 iexplore 969 35 74868 87576 328 43.92 7480 iexplore 404 10 7280 640 6.23 2812 1368 kavstart 65 3 3360 46 0.09 316 6592 kissvc 80 5 1012 62 1.28 496 kmailmon

The above is just one of the powerful functions of Windows powershell. In addition, it can interact with WMI, Access Machine devices, and format output results.


This article briefly introduces windows powershell. We believe that these introductions have seen the power of Windows powershell, and powershell can be used for management in Microsoft products.

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: 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.