Start PowerShell (1): What Can PowerShell do?

Source: Internet
Author: User

1. interact with the file system and run applications

Just like in Dos, enter "dir" on the PowerShell interactive interface and press enter to display the subfolders and file information in the current folder.

?PS D:\Projects\Practise\PowerShell> dirDirectory: D:\Projects\Practise\PowerShellMode LastWriteTime Length Name---- ------------- ------ ----d---- 1/23/2013 12:35 PM d1d---- 1/23/2013 12:35 PM d2-a--- 1/21/2013 8:38 PM 36314 alias.tx-a--- 1/21/2013 8:32 PM 241530 cmdlets.-a--- 1/18/2013 10:18 AM 888 errors.t

There are many commands like this, such as echo "Hello", cd... and so on. From this point of view, it is a command line that implements the original command line function. But is PowerShell an enhanced version of the command line, or is it a superset of the command line? This is really not the case. I will discuss this issue in the future.

Just like running an application in the command line, you can run the application in the PowerShell interaction window as follows:

? Ps c: \ Users \ v-lukez> notepadPS C: \ Users \ v-lukez> to better control the application, run the start Command, as shown below :? Ps c: \ Users \ v-lukez> start notepad-WindowStyle MaximizedPS C: \ Users \ v-lukez>

The above results can maximize the Notepad window. In fact, there are many similar commands, and more parameters mean more precise control.

Ii. Create a script

Task automation is based on program files or executable script files. PowerShell also supports making command lists into script files for execution. The content of the Hello. ps1 script file is as follows:

? $ A = "Hello" $ aecho $ a> a.txt dir a.txt Hello. ps1 script file execution result :? Ps e: \> D: \ Projects \ Practise \ PowerShell \ Hello. ps1HelloDirectory: E: \ Mode LastWriteTime Length Name ---- ------------- ------ ----- a --- 1/30/2013 56 PM 16 a.txt

You may find that the PowerShell script file is with the. PS1 extension. The above script is very simple: first define a variable, then output the result of this variable, then write the value of this variable into the file a.txt, and finally output the attribute information of this file. From this point of view, PowerShell scripts are similar to Batch files. But in fact, PowerShell can do more.

PowerShell scripts support user-defined functions, as we do in programming languages. FuncDemo. ps1 provides an example of writing a function in a PowerShell script:

?#funcDemo.ps1function SayHello ($name){echo "hello $name"}SayHello "Luke"

The running result of the script is "Hello Luke ".

In addition, for the running sequence of the internal statements of PowerShell scripts, a simple description is provided here: in addition to the Function Definition, commands or function calls in the script are also equivalent to executing commands in sequence; the statements in a function are executed only when the function is called.

 Iii. Use the. Net type and COM Object

Being able to take advantage of the. Net Type and COM object is the biggest feature of PowerShell, which allows PowerShell to take advantage of existing resources to the maximum extent, and it is easy to attract. Net and COM programmers to their own place.

Simple Type:

? [Int] $ a = 10 [String] $ B = 10. Net type? $ Message = new-object Net. Mail. MailMessage ("me@source.com", "you@destination.com", "Subject", <br> "Here is some email") COM object? $ MyWord = new-object-comobject Word. Application

After creating. Net or COM objects, you can use the attributes and methods of these objects to perform more complex operations.

As a summary of this article, PowerShell is one of the daily essential tools for Windows administrators.

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.