PowerShell is a next-generation command-line shell that is more powerful and easier to use than its predecessor (Cmd.exe). The fundamental difference is that it is an object-based operation (based on the. NET Framework) rather than a string-based operation.
This tool can be used alone and can completely replace Cmd.exe. For example:
But it's much more than that, for example, we can easily get all the process names
Let's take another look at the following example to get a list of currently running services. (can be easily filtered with conditions)
In addition, PowerShell supports customization, such as Microsoft's many products provide dedicated PowerShell plug-ins (typically: SQL Server,sharepoint server, Exchange server, etc.)
The management of the server can be achieved through these special enclosures. Features are very powerful. For example, the following sqlps can view a database like a folder
Another example of EMS (Exchange managment Shell), you can modify an address list
It looks pretty good, for more details about PowerShell, if you're interested, you can refer to Microsoft's documentation.
Next talk about another topic, PowerShell is so powerful, but after all it is manual operation, can you call it in the program, and perform the relevant operation?
The answer is: yes.
Let's look at a small example
Add a reference. This assembly is in the C:\Program Files (x86) \reference assemblies\microsoft\windowspowershell\v1.0 Directory
Write the following simple code
Using System;Using System.Collections.Generic;using System.Linq; using System.Text; using System.Management.Automation; using System.Management.Automation.Runspaces; using System.Diagnostics; namespace powershellinvoker{ class Program { static void Main (string[] args) {var runspace = Runspace Factory.createrunspace (); Runspace. Open (); var piple = runspace. Createpipeline ("get-process"); var result = Piple. Invoke (). Select (p = = p.baseobject). Cast<process> (); foreach (var item in result) {Console.WriteLine ("{0}\t{1}\t{2}", item. Id.tostring (). PadRight (+), item. Processname.padright (+), item. Threads.count); } console.read (); } }}
Yes, PowerShell is a. NET framework-based object operation
Introduction to PowerShell and its programmatic access