What can PowerShell do? As mentioned in the preamble, PowerShell first is a shell that defines a bunch of commands and operating systems, especially interacting with file systems, enabling applications to start and even manipulate applications; second, PowerShell allows several commands to be grouped together and executed in a file. Implement file-level reuse, that is, the nature of the script; third, PowerShell can leverage. NET types and COM objects to simply interact with a variety of systems to accomplish complex, automated operations.
One, interact with the file system, run the application
As in dos, typing "dir" and carriage return on the PowerShell interface displays the subfolders and file information under the current folder.
Copy Code code as follows:
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 a lot of commands like this, such as echo "Hello", CD. Wait a minute. From here it is a command line that implements the functionality of the original command line. But is the PowerShell the command line, a superset of the command line? This is really not, later will be specifically to say this question.
Just like running an application on the command line, you can run the application in the PowerShell interactive window as follows:
Copy Code code as follows:
PS c:\users\v-lukez> Notepad
PS c:\users\v-lukez>
If you want to better control your application, you can use the start command as follows:
Copy Code code as follows:
PS c:\users\v-lukez> start Notepad-windowstyle maximized
PS c:\users\v-lukez>
The above results can achieve the maximum of the Notepad window. In fact, there are a lot of similar commands, and more parameters mean more granular control.
Second, create a script
Automation of tasks is based on program files or executable script files, and PowerShell also supports the execution of a command list as a script file. The following is the contents of the Hello.ps1 script file:
Copy Code code as follows:
$a = "Hello"
$a
echo $a > A.txt
Dir a.txt
The execution of the Hello.ps1 script file results in the following:
Copy Code code as follows:
PS e:\> D:\Projects\Practise\PowerShell\Hello.ps1
Hello
Directory:e:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-A---1/30/2013 4:56 PM a.txt
As you may have noticed, the PowerShell script file is extended with. 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 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.
The PowerShell script supports custom functions, just as we do in the programming language. FUNCDEMO.PS1 gives an example of writing a function in a PowerShell script:
Copy Code code as follows:
#funcDemo. PS1
function SayHello ($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, the commands in the script or function calls (which in fact are equivalent to executing commands) are executed sequentially; the statements in a function are executed only when the function is called.
Third, use. NET Types and COM objects
able to use. NET types and COM objects are the PowerShell features that allow PowerShell to make maximum use of existing resources and are easily accessible. NET and COM programmers to attract their own.
Simple Type:
Copy Code code as follows:
[int] $a = 10
[String] $b = 10
. NET Type
Copy Code code as follows:
$Message = New-object Net.Mail.MailMessage ("me@source.com", "you@destination.com", "Subject", <br> "here is" some Email ")
COM objects
Copy Code code as follows:
$myWord = New-object-comobject Word.Application
was created. NET or COM objects, you can use the properties and methods of these objects to accomplish more complex operations.
As the end of this article, summed up in a sentence, PowerShell eat very little, work a lot of Windows administrator is one of the daily necessary tools. Pass by, don't miss Ah ...