It's too late to post a blog post today. Thank you for your continued attention!
This section describes the objects, basic formats, and parameters in PowerShell. It is still the foundation of PowerShell.
PowerShell objects
As we said at the beginning of this tutorial, PowerShell is based on object-oriented, not as text-based as traditional shell. The main reason for this is that the Win platform mainly focuses on Object-Oriented management operations. Therefore, PowerShell inherits this feature in order to meet the system characteristics and our operation habits. Therefore, unlike the traditional shell, in PowerShell, We can freely interact with objects,
What isObject
I wonder if you have any development experience. In fact, the concept of object-oriented is proposed to better use programming languages to solve practical problems.
If you are not talking nonsense, you can directly get into the subject. In PowerShell, "object" refers to the behavior in which we collect information or perform operations. Includes attributes (information, which can be collected) and methods (which can be executed ).
There is a vivid example: "bulb ". The object is obvious. It is a light bulb. The properties of a bulb may include its color, power, and type (fluorescent, incandescent, or halogen ). For its operations, or methods, it is what we can execute, such as opening and closing. This is easy to understand!
Let's take a look at the attributes and methods of an object in PowerShell.
First, you may frequently use it "Get-Member". This cmdlet is used to check the attributes and methods of an object.
For example:
get-service | get-member
You can use this command to view the properties and methods of "get-service" t. In this example, the pipeline operator is used for Command Transmission. The running result is as follows:
Of course, we can use the "get-member" parameter to view all attribute class objects or method class objects of "get-service.
For example:
View All property class objects of "get-service"
Get-Service | Get-Member -MemberType Property<enter>
View All method class objects of get-service
Get-Service | Get-Member -MemberType Method<enter>
Why are we so emphasizing objects? The reason is that in PowerShell, everything is an object.
For example:
To find out which files are written to d: \ on a specified date, run the following command:
Get-ChildItem -Path d:\ -Recurse | Where-Object {$_.LastWriteTime -gt "01/01/2010"}<enter>
Now let's explain:
First, "get-childitem" is used to enumerate our file system. Use the "-path" parameter to point the path to "d: \" and use the "-recurse" parameter, it means that all files are displayed, and even sub-directories are displayed. Next, we pass the results to the where-object loop declaration using the pipeline to filter out the results that meet the conditions.
So what is "lastwritetime?
Run the following command to check the attributes of "get-childitem" for filtering:
get-chileitem | get-member
You can find one of them:
Yes. The object attribute to be filtered out is the last write date. In the subsequent definitions, we can see that "LastWriteTime" uses a "Syetem. DateTime" data type as feedback. Therefore, we use "-gt" to further filter the last half of the statement. "-gt" is the abbreviation of "greater than", which means "greater ". In future tutorials, I will introduce more such operations. As mentioned above, "LastWriteTime" is a "Syetem. DateTime" type data. Therefore, we use expressions like "01/01/2010. Pay more attention to this point, and pay attention to data types in future use.
In the subsequent tutorials, I will give a full introduction to WMI, COM, And. NET as much as possible. However, it is sufficient to know and master the above.
PowerShell format
In this section, I will introduce the formatting output in PowerShell. When we use a cmdlet, the parameter "format-" allows us to select a habitual output mode. Run the following command to try it out:
Get-Command Format-* <enter>
The result is:
Well, this knowledge point is very simple. Please try the following command for your children's shoes. You can see the result.
get-childitem c:\windows | format-table <enter>get-childitem c:\windows | format-table -autosize <enter>get-childitem c:\windows | format-custom <enter>get-childitem c:\windows | format-list <enter>get-childitem c:\windows | format-list -Property FullName <enter>get-childitem c:\windows | format-wide <enter>
Of course, I don't want to explain too much about the complex things below. If you are willing to give it a try, you can understand it at a glance.
Get-ChildItem C:\Windows -Recurse | Format-List -Property FullName,CreationTime,LastWriteTime<enter>
Get-ChildItem C: | Format-Wide -Column 3<enter>
In addition, there are outputs in other formats in other cmdlet. For example, in "get-process", "group-object", "Get-EventLog", we may use "Sort-Object", or even, you can output a file in a specific format. For example, you can use "convertory-HTML" to output an html file and "Export-CSV" to output a table file (which can be opened in Excel ).
An example is as follows (remember the pipeline operator ):
Get-Process | Group-Object Company<enter>
Get-EventLog System | Group-Object eventid<enter>
Get-EventLog System | Group-Object eventid | Sort-Object Count -descending<enter>
Get-Process | ConvertTo-html<enter>
Get-Process | ConvertTo-html | out-file “Processes.html”<enter>
Get-Process | Export-CSV Processes.csv<enter>
To open a file, run the following command:
Invoke-Item Processes.html<enter>
Invoke-Item Processes.csv<enter>
Check it out (the output is a ". CSV" file ):
Run the "Invoke-Item" command to open:
Invoke-Item Processes.csv <Enter>
Is it easy to format the Output Using PowerShell? I personally think it is easier to get started than VBScript. The management system is more convenient!
Common PowerShell Parameters
As we mentioned earlier, PowerShell uses a new "verb-noun" naming method for the cmdlet to simplify our memory, almost all cmdlets have unified standard parameters. Of course, as I said, almost all -- not all. The following list lists public parameters (the names of these parameters cannot be customized ):
-Confirm prompts the user before executing the cmdlet.
-Debug provides debugging information.
-ErrorAction indicates an error that may occur when the cmdlet executes an operation. For example, continue or stop.
-ErrorVariable uses a specific variable ($ error) to save the error message.
-OutVariable: The variable used to save output information.
-OutBuffer determines the number of objects to be cached before the next pipeline is passed.
-Verbose provides us with more details.
-Whatif does not actually execute the cmdlet, but only tells you what will happen.
In addition, PowerShell retains the following aliases for these parameters: vb, db, ea, ev, ov, and ob.
Too many parameters are hard to remember? Well, remember to use the "Tab" key, for example:
get-service -<Tab>
Or use the help command "get-help ":
get-help get-service -full <Enter>
Let's make some simple demos:
Set-ExecutionPolicy Unrestricted -whatif <enter>
Is it convenient? Before executing the cmdlet, "-whatif" will tell you what will happen next.
The following cmdlet is used:
Set-ExecutionPolicy Unrestricted -confirm <enter>
Yes, it will return a verification operation to get further permission from the user. Only "Y", "A", "N", "L", and "? "We can understand it easily. What about" S?
Observe the following execution results:
Set-ExecutionPolicy Unrestricted -confirm<enter>
Are you sure you want…
S<enter> (places the prompt in suspend mode as denoted by “>>”).
>>Get-ExecutionPolicy<enter>
Resricted (or whatever the policy is set to).
>>exit<enter> (Typing “exit” leaves suspend mode and returns to the original command)
Are you sure you want…
Y<enter> (Confirms “Yes” and sets the ExecutionPolicy to “Unrestricted”).
Execution instance:
Smart, do you understand?
Now, the content of this section is here. In this section, the introduction of objects, formats, and parameters in PowerShell is only the tip of the iceberg. In the subsequent tutorials, we will continue to use what we learned today. Of course, there are many other content and parameters that we have not yet covered in this section. I hope that you will be able to actively explore using the tools provided in PowerShell, such as the "get-help" and "get-member" commands.
Ps: Finally, thank you very much for your enthusiastic and continuous attention to this tutorial. If you have any comments or questions, please leave a message and I will give you a detailed answer! Thank you.