Start powershell (1): What Can powershell do?

Source: Internet
Author: User

What can powershell do? As mentioned in the preface, powershell is a shell, which defines a bunch of commands and operating systems, especially interaction with file systems, to enable applications.Program, Or even manipulate the application. Second, powershell allows several commands to be combined and put into a file for execution to achieve file-Level Reuse, that is, scripts. Third, powershell can be fully utilized. net Type and COM object, to easily interact with a variety of systems, complete a variety of complex, automated operations.

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> dir Directory: D: \ projects \ practise \ powershellmode lastwritetime length name ---- ------------- ------ ---- D ---- 1/23/2013 PM D1d ---- 1/23/2013 PM d2-a --- 1/21/2013 PM 36314 alias. TX-A --- 1/21/2013 8:32 PM 241530 cmdlets. -A --- 1/18/2013 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 follows:

 
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

The execution results of the hello. PS1 script file are as follows:

Ps e: \> D: \ projects \ practise \ powershell \ hello. ps1hello Directory: e: \ mode lastwritetime length name ---- ------------- ------ -----A --- 1/30/2013 4: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.Programming Language. 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 (which is also equivalent to executing commands) are executed 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 ",
"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. Never miss it...

 

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.