Powershell is capable: 1. Can interact with the file system, start applications, and even manipulate applications; 2. Allows several commands to be combined and executed in files to achieve file-level reuse, that is It is said that you can create scripts; 3. You can make full use of .Net types and COM objects to simply interact with various systems and complete various complex and automated operations.
The operating environment of this tutorial: Windows 10 system, Dell G3 computer.
What is PowerShell?
PowerShell is a cross-platform task automation solution consisting of a command line shell, a scripting language, and a configuration management framework. PowerShell runs on Windows, Linux, and macOS.
Command Line Shell
PowerShell is a modern command shell that includes the best features of other commonly used shells. Unlike most shells, which only accept and return text, PowerShell accepts and returns .NET objects. The shell includes the following features:
Reliable command line history
Tab autocompletion and command prediction (see about_PSReadLine)
Support command and parameter aliases
Pipe for chaining commands
In-console help system, Similar to the Unix man page
#What can PowerShell do?
PowerShell is first of all a Shell, which defines a bunch of commands to interact with the operating system, especially the file system, and can start applications and even manipulate applications; secondly, PowerShell allows Several commands are combined and executed in a file to achieve file-level reuse, which means it has the nature of a script; third, PowerShell can make full use of .Net types and COM objects to simply interact with various systems and complete various tasks. A complex, automated operation.
1. Interact with the file system and run the application
Just like in Dos, type "dir" on the PowerShell interactive interface and press Enter. Displays subfolder 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
There are many commands like this, such as echo "Hello", cd...etc. From here it seems that it is a command line, realizing the functions of the original command line. But is PowerShell an enhanced version of the command line? Is it a superset of the command line? This is really not the case. I will talk about this issue specifically in the future.
Just like running applications in the command line, you can run applications in the interactive window of PowerShell, as follows:
PS C:\Users\v-lukez> notepad PS C:\Users\v-lukez>
If you want to have better control over the application Program, 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 maximize the Notepad window. In fact, there are many similar commands, and more parameters mean more precise control.
2. Create a script
The automation of tasks is based on program files or executable script files. PowerShell also supports making a command list into a script file for execution. . The following is the content of the Hello.ps1 script file:
$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 Yes, PowerShell script files have a .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 to the file a.txt, and finally output the attribute information of this file. From this point alone, PowerShell scripts are very similar to Batch files. But in reality, PowerShell can do much more.
PowerShell scripts support custom functions, just like we do in programming languages. funcDemo.ps1 gives an example of writing a function in a PowerShell script:
#funcDemo.ps1 function SayHello ($name) { echo "hello $name" } SayHello "Luke"
The result of the script is "Hello Luke".
In addition, regarding the running order of statements within the PowerShell script, here is a brief explanation: In addition to function definitions, commands or function calls in the script (actually equivalent to executing commands) will be executed sequentially; The statement will only be executed when the function is called.
3. Utilizing .Net types and COM objects
Being able to utilize .Net types and COM objects is the biggest feature of PowerShell, which allows PowerShell to maximize the use of existing It has resources and can easily recruit .Net and COM programmers to its staff.
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 properties and methods of these objects to complete more complex operations.
To end this article, let me sum it up in one sentence: PowerShell eats very little and does a lot of work. It is one of the daily essential tools for Windows administrators. If you are passing by, don’t miss it. . .
How to open and run PowerShell
1. Win menu key R:
2. The running window pops up as follows:
3. Enter PowerShell in the input box and press Enter or click OK:
4. After typing, the PowerShell command window will pop up. Congratulations, you have done it. You can directly enter the command line in it. Please refer to the following:
For more related knowledge, please visit FAQ column!
The above is the detailed content of What is powershell used for?. For more information, please follow other related articles on the PHP Chinese website!