Powershell object selection, sorting, and variable Storage

Source: Internet
Author: User
Basic powershell tutorial (17) -- object selection, sorting, and variable Storage

You can use the select-object cmdlet to create a new and custom windows powershell object. The latter contains the attributes selected from the objects used to create them. Enter the following command to create a new object, which only contains the name and freespace attributes of the win32_logicaldisk WMI class:

PS> Get-wmiobject-class win32_logicaldisk | select-object-property name, freespace

 

Name freespace

-------------

C: 50664845312

 

After the command is issued, you cannot view the data type. However, if you run the select-object command and pass the result to get-member through the pipeline, you can find that, the new object type pscustomobject already exists:

PS> Get-wmiobject-class win32_logicaldisk | select-object-property name, freespace | get-member

 

 

Typename: system. Management. Automation. pscustomobject

 

Name membertype Definition

------------------------

Equals method system. boolean equals (Object OBJ)

Gethashcode method system. int32 gethashcode ()

GetType method system. Type GetType ()

Tostring method system. String tostring ()

Freespace noteproperty freespace =...

Name noteproperty system. string name = C:

 

Select-object has many functions. One of them is to copy the data that can be modified subsequently. Now we can solve the problems we encountered in the previous section. We can update the freespace value in the newly created object, and the output will include descriptive labels:

Get-wmiobject-class win32_logicaldisk | select-object-property name, freespace | foreach-object-process {$ _. freespace = ($ _. freespace)/1024.0/1024.0; $ _}

Name freespace

-------------

C: 48317.7265625

 

Sort objects

You can use the sort-object cmdlet to organize the displayed data and scan the data more easily. Sort-object can obtain the names of one or more attributes for sorting and return data sorted by these attribute values.

Take the win32_systemdriver instance list as an example. To sort by State and by name, you can run the following command:

Get-wmiobject-class win32_systemdriver | sort-object-property state, name | format-table-property name, state, started, displayname-autosize-wrap

 

Although this will generate a long data display, it can still be seen that items with the same status are combined:

Name state started displayname

---------------------------

ACPI running true Microsoft ACPI driver

AFD running true AFD

Amdk7 running true amd K7 processor driver

Asyncmac running true Ras asynchronous media driver

...

Abiosdsk stopped false abiosdsk

Acpiec stopped false acpiec

AEC stopped false Microsoft Kernel Acoustic Echo Canceller

...

 

You can also specify the descending parameter to sort objects in reverse order. This will reverse the order, so the names will be sorted in reverse alphabetical order, and numbers will be sorted in descending order.

PS> Get-wmiobject-class win32_systemdriver | sort-object-property state, name-descending | format-table-property name, state, started, displayname-autosize-wrap

 

Name state started displayname

---------------------------

Ws2ifsl stopped false windows socket 2.0 non-ifs service provider support environment

 

Wceusbsh stopped false Windows ce usb serial host driver...

...

Wdmaud running true Microsoft winmm WDM audio compatibility driver

Wanarp running true remote access ip arp driver

...

 

Store objects with variables

Windows powershell can use objects. With Windows powershell, you can create variables (essentially named objects) to keep the output for future use. If you are used to processing variables in other shells, remember that Windows powershell variables are objects rather than text.

The variable can always be specified by the first character $, and the variable name can contain all letters, numbers, and underscores.

Create variable

You can create a variable by entering a valid variable name:

PS> $ Loc

PS>

 

Because $ LoC has no value, no results will be returned. You can create variables and assign values to them at the same time. Windows powershell can only create a variable that does not exist currently. Otherwise, it will assign the specified value to an existing variable. To store the current location in the variable $ Loc, type:

$ Loc = Get-location

 

Because the output has been sent to $ Loc, no output is displayed after you type this command. In Windows powershell, the output displayed is actually an additional function, because unoriented data is always sent to the screen. Type $ LOC to display the current location:

PS> $ Loc

 

Path

----

C:/temp

 

You can use get-member to display information about variables. $ LOC is passed to get-member through the pipeline, and $ LOC is a pathinfo object, which is similar to the get-location output:

PS> $ loc | get-member-membertype Property

 

 

Typename: system. Management. Automation. pathinfo

 

Name membertype Definition

------------------------

Drive property system. Management. Automation. psdriveinfo drive {Get ;}

Path property system. String path {Get ;}

Provider property system. Management. Automation. providerinfo provider {...

Providerpath property system. String providerpath {Get ;}

 

Operate on variables

Windows powershell provides several commands used to operate variables. You can enter the following command to view the complete readable list:

Get-command-Noun variable | format-table-property name, definition-autosize-wrap

 

In addition to the variables created in the current Windows powershell session, there are also several system-Defined variables. You can use the remove-variablecmdlet to clear all variables that are not controlled by Windows powershell. Run the following command to clear all variables:

Remove-variable-name *-force-erroraction silentlycontinue

 

This will generate a confirmation prompt, as shown below:

Confirm

Are you sure you want to perform this operation?

Perform the "Remove variable" Operation on the target "Name: error ". [Y] Yes [a] All [N] No [l] All [s] suspended [?] Help (the default value is "Y"):

 

Then, if you run the get-variable cmdlet, you can view the remaining windows powershell variables. Because a Windows powershell variable drive exists, you can also enter the following command to display all windows powershell variables:

Get-childitem variable:

 

Use the cmd.exe variable

Although Windows powershell is not cmd.exe, it also runs in the command shell environment and can use the same available variables in any Windows environment. These variables are made public through a drive named env. Run the following command to view these variables:

Get-childitem env:

 

Although standard variable cmdlet is not designed to process env: variables, you can still specify to use these variables. For example, to view the root directory of the operating system, type the following command to use the command shell program % SystemRoot % variable in Windows powershell:

PS> $ ENV: systemroot

C:/Windows

 

You can also create and modify environment variables in Windows powershell. Environment Variables accessed from Windows powershell comply with standard rules for environment variables outside windows.

 

 

Note: This article references the basic powershell tutorial of the csdn blog erway (17 )--Object selection, sorting, and variable Storage

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.