Return to the pleasure of Dos operations and enter the PowerShell world

Source: Internet
Author: User

Write down this article from 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. NET Framework allows IT professionals and developers to control and automatically manage Windows and applications. 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 piece of code from all processes in the current system in. NET:

static void Main(string[] args){    Process[] proceses = Process.GetProcesses();    foreach (var item in proceses)    {        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("")$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    59     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   2508 dwm    2261      48    75040      72600   416   341.38   2548 explorer      0       0        0         24     0               0 Idle      664      28    36696      49076   173    14.78   3384 iexplore    969      35    74868      87576   328    43.92   7480 iexplore    404      10     7280        640    96     6.23   2812 kavstart      65       3     1368       3360    46     0.09    316 kissvc     80       5     6592       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.