Start PowerShell (1): What Can PowerShell do ?, Getting started with powershell

Source: Internet
Author: User

Start PowerShell (1): What Can PowerShell do ?, Getting started with powershell

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

 

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, and can start and even manipulate applications; 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\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

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

PS C:\Users\v-lukez>

To better control the application, run the start command as follows:

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

PS 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 AutomationIt is based on program files or executable script files. PowerShell also supports making the command list a script file for execution. The content of the Hello. ps1 script file is as follows:

$a = "Hello"

$a

echo $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.ps1

Hello

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, as we do in programming languages. FuncDemo. ps1 provides an example of writing a function in a PowerShell script:

#funcDemo.ps1

function 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", <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. 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.