Explore the object, format and parameters of PowerShell (d) PowerShell _powershell

Source: Internet
Author: User
Tags datetime

Today posted blog late, thank you for your continued attention!

This section will give you an introduction to the objects, basic formats, and parameters under PowerShell. Still belongs to the foundation of PowerShell.

Objects in the PowerShell

As we said at the beginning of this tutorial, PowerShell is based on object-oriented and not text-based as the traditional shell. The main reason is because the win platform in the management of the main object-oriented, so in order to conform to the characteristics of the system and our operating habits, PowerShell also inherited this feature. So unlike the traditional shell, in PowerShell, we can interact with objects randomly,

First come to know, what is object--object

I do not know whether you have engaged in development experience. In fact, the concept of object-oriented is proposed in order to solve practical problems with program language better.

Don't say much nonsense, cut to the chase directly. In PowerShell, "object" refers to the act of gathering information or performing an action. Include attributes (information that we can collect) and methods (we can execute).

There is a vivid example--"bulb". The object is obvious and it is a light bulb. The properties of a light bulb may include its color, power, and type (fluorescent, incandescent or halogen lamps). For it to operate, or to call it a method, is the behavior that we can perform, such as opening and closing. It's easy to understand!

Let's look at the properties of an object in PowerShell and its methods.

First, you may often use it "Get-member", which is used to check which attributes and methods an object has.

For example:

Get-service | Get-member

Use this command to view the properties and methods of the "Get-service" T. In this example, we use a pipe character to deliver the command. The results of the operation are as follows:

Of course, we can use the parameters of "Get-member" to view all the attribute class objects of "Get-service", or the method class object.

For example:

View all attribute class objects for "Get-service"

Get-service | Get-member-membertype property<enter>

View all method class objects for Get-service

Get-service | Get-member-membertype method<enter>

Why do we emphasize objects so much? The reason is that in PowerShell, everything is an object.

For example:

We want to find out what files are written to d:\ on the specified date, using the following command:

Get-childitem-path D:\-recurse | Where-object {$_. Lastwritetime-gt "01/01/2010"}<enter>

Now to explain:

First, "Get-childitem" is used to enumerate our file systems, using the "-path" argument, pointing the path to "D:\", using the "-recurse" parameter, meaning that all files, even subdirectories, will be displayed. Next, we use the pipe to pass the result to the circular declaration "Where-object" to filter out the results that match the criteria.

So what is "LastWriteTime"?

We use the following command to see what attributes of "Get-childitem" are available for our filtering:

Get-chileitem | Get-member

One of the following can be found:

Yes, that's it. The object attribute we need to filter out is the last written date. As you can see in the following definition, "LastWriteTime" Takes a "syetem.datetime" data type as feedback. Therefore, in the second half of the entire statement, we use "-gt" for further filtering, "-GT" is the abbreviation for "greater than", meaning "greater than". I'll look at more of these things in a future tutorial. As mentioned earlier, "LastWriteTime" is a "syetem.datetime" type of data, so we end up using an expression like "01/01/2010". This requires more attention, in the future application of the need to pay attention to data types.

In subsequent tutorials, I will also be able to introduce WMI, COM, and. NET as comprehensively as possible, but we now know and master the above is enough.

Format of PowerShell

In this section, I'll introduce the formatted output in PowerShell. When we use a cmdlet, the parameter "format-" allows us to choose a custom output pattern. Use the following command to try:

Get-command format-* <enter>

The results are:

Well, this one knowledge point is very simple. Please use the following command children's shoes to try, the results of how to look at the know.

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, the more complicated and the following, I do not want to explain too much, everyone as long as willing to do a hands-on try, a glance can be seen.

Get-childitem C:\Windows-Recurse | Format-list-property fullname,creationtime,lastwritetime<enter>

Get-childitem C: | Format-wide-column 3<enter>

In addition, in other cmdlet, there are other formats of output. For example, in "get-process" there is "Group-object", "Get-eventlog" in which we may use "sort-object", or even, we can output files in a particular format, such as using "convertto-html" Output is HTML, using "Export-csv" output as a table file (you can open it using Excel).

All examples are as follows (remember the pipe character):

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 the file, use the following command:

Invoke-item processes.html<enter>

Invoke-item processes.csv<enter>

Take a look at the screenshot (output is ".) CSV "file):

Open using the Invoke-item command:

Invoke-item Processes.csv <Enter>

Is it easy to use PowerShell formatted output? Personally think it's easier to get started than VBScript. Management system more convenient!

Common parameters for PowerShell

As we mentioned earlier, in order to simplify our memory, PowerShell used a new "verb-noun" naming method for the cmdlet, making it easier for almost all of the cmdlet to have uniform standardized parameters, and of course, I said, almost everything-not all. The following list lists "public parameters" (the names of these parameters are not customizable):

-confirm prompts the user before executing the cmdlet.

-debug provides relevant debugging information.

-erroraction prompts the cmdlet for an error that may occur when performing an operation. Such as: Continue, stop, etc.

-errorvariable uses a specific variable ($error) to save the error message.

-outvariable the variable used to hold the output information.

-outbuffer determines the number of objects that need to be cached before the next pipeline pass.

-verbose to provide us with more details.

-whatif does not actually execute the cmdlet, just tells you what will happen.

In addition, the following aliases for these parameters are retained in PowerShell: VB, DB, EA, Ev, OV, and OB.

Too many parameters to remember? Good, remember to use the "tab" key, such as:

Get-service-<tab>

or use the help command "Get-help":

Get-help Get-service-full <Enter>

OK, let's do some simple demo:

set- ExecutionPolicy Unrestricted-whatif <enter>

Is it convenient? Before you perform a cmdlet, "-whatif" will tell you what will happen next.

So what about this cmdlet:

set- ExecutionPolicy unrestricted-confirm <enter>

Yes, it will return a validation operation to obtain further permission from the user. Just "Y", "A", "N", "L" and "?" We can all easily understand, then "S"?

Note the following execution results:

Set-executionpolicy unrestricted-confirm<enter>

Are you sure your 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 your want ...

Y<enter> (confirms "Yes" and sets the ExecutionPolicy to "unrestricted").

Execution Instance screenshot:

You're smart, you got it?

Okay, here's the section. The introduction of objects, formats, and parameters in this section is just the tip of the iceberg, and we will continue to use today's PowerShell to work on the following tutorials. Of course, in this section there are many other things we have not yet involved in the content, parameters, etc., I hope that children's shoes can actively use the tools provided in PowerShell exploration, such as "Get-help", "get-member" command.

Ps: Finally, thank you very much for your warm and continuous attention to this tutorial, if you have any comments or questions, please leave a message, I will do a detailed answer! Thank you.

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.