Getting Started with PowerShell (a): What can PowerShell do?

Source: Internet
Author: User
Tags function definition

Original link: http://www.cnblogs.com/ceachy/archive/2013/01/30/WhatCanPowerShellDo.html

What can PowerShell do? As mentioned in the preamble, PowerShell is first a shell, defining a bunch of commands and operating systems, especially interacting with the file system, enabling the application to be started, and even manipulating the application; second, PowerShell allows several commands to be combined to execute in a file, Implement file-level reuse, that is, the nature of scripting; third, PowerShell can take full advantage of. NET types and COM objects to easily interact with various systems to accomplish a variety of complex, automated operations.

I. Interacting with the file system, running the application

Just like in DOS, type "dir" on the PowerShell interface and enter it to display subfolders and file information under the current folder.

PS D:\Projects\Practise\PowerShell> dir

Directory: D:\Projects\Practise\PowerShell

Mode                LastWriteTime     Length Name

----                -------------     ------ ----

d----         1/23/2013  12:35 PM            d1

d----         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

Commands like this are many, such as echo "Hello", CD. Wait a minute. From here it appears to be a command line that implements the original command line functionality. But is powershell the command line's enhanced version, a superset of the command line? This is really not a problem that will be specifically said later.

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

PS C:\Users\v-lukez> notepad

PS C:\Users\v-lukez>

If you want to control your application better, you can use the Start command, as follows:

PS C:\Users\v-lukez> start notepad -WindowStyle Maximized

PS C:\Users\v-lukez>

The above results can realize the maximization of the Notepad window. In fact, there are many similar commands, and more parameters mean finer control. Second, create the script

  Automation of tasks is based on program files or executable script files, and PowerShell supports the execution of command lists as script files. The following is the contents of the Hello.ps1 script file:

$a = "Hello"

$a

echo $a > a.txt

dir a.txt

The execution of the Hello.ps1 script file results in the following:

PS E:\> D:\Projects\Practise\PowerShell\Hello.ps1

Hello

Directory: E:\

Mode                LastWriteTime     Length   Name                                                                     

----               ------- ------    ----------                                                                         

-a---         1/30/2013   4:56 PM         16 a.txt                                                                   

You may have found that the PowerShell script file is an extension of. PS1. The above script is very simple: first define a variable, then output the result of the variable, then write the value of the variable to the file a.txt, and finally output the file's property information. From this point of view, PowerShell scripts are much like batch files. But in fact, PowerShell can do more.

PowerShell scripts support custom functions, just as we did in the programming language. FUNCDEMO.PS1 gives an example of writing a function in a PowerShell script:

#funcDemo.ps1

functionSayHello ($name)

{

echo "hello $name"

}

SayHello "Luke"

The script runs as "Hello Luke."

In addition, there is a simple explanation for the order in which the internal statements of the PowerShell script are run: In addition to the function definition, commands in the script or function calls (which are actually equivalent to executing commands) are executed sequentially; the statements in the function are executed only when the function is called. Third, use. NET Types and COM objects

be able to use. NET type and COM objects are the biggest features of PowerShell, which allows PowerShell to make the most of existing resources and easily put.  NET and COM programmers to attract their own. Simple Type:

[int]$a = 10

[String]$b = 10. NET Type

$Message = new-object Net.Mail.MailMessage("[email protected]","[email protected]", "Subject", <br>  "Here is some email")COM object

$myWord = new-object -comobject Word.Application

was created. NET or COM objects, you can take advantage of the properties and methods of these objects to accomplish more complex operations.

As this article ends, in a nutshell, PowerShell eats very little and works a lot, and is one of the daily essentials of Windows administrators. Walk past, don't miss Ah ...

Getting Started with PowerShell (a): What can PowerShell do?

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.