Windows PowerShell uses a hash table _powershell

Source: Internet
Author: User
Tags hash

A hash table is a pair that is no longer restricted to the use of digital addressing in the hash tables, and can be addressed with any type of data type.

Creating a hash table
Before using @ () to create an array, now use @{} to create a hash table, using the key of the hash table to access the corresponding value.

PS c:powershell> $stu =@{Name = "Xiaoming"; Age= "A" sex= "man"}
PS c:powershell> $stu

name              Value
----              -----
name              xiaoming
Age
Sex              male

PS c:powershell> $stu ["Name"]
xiaoming
PS c:powershell> $stu ["Age"]
PS c:powershell> $stu. Count
3
PS c:powershell> $stu. The Keys
Name age
sex
PS c:powershell> $stu. Values
Xiao
Ming
Man

Storing arrays in a hash table

You can use arrays when you create a hash table, because creating an array and a hash table's element keywords does not conflict. One is a comma and one is a semicolon.

PS c:powershell> $stu =@{Name = "Xiaoming"; "Age="; sex= "male"; Books= "The Kingdoms", "Siege", "Hamlet"}
PS c:powershell> $stu

Name              Value
---------Books {The             kingdoms of the , Fortress besieged, Hamlet
}              Name
xiaoming
sex              male

Inserts a new key value in the hash table

Inserting a new key value in a hash table is convenient, like defining a variable, which you can use directly to

PS c:powershell> $Student =@{}
PS c:powershell> $Student. name= "make Fox Dash"
PS c:powershell> $ Student.school= "Huashan pie"
PS c:powershell> $Student

name              Value
----              -----
name              Make Fox Chong
School             Huashan Pie

Update and delete of hash table values

If you want to update the value of a key, you can override it directly. If you want to remove this key-value pair, you can use the Remove method, which is the key

PS c:powershell> $stu

name              Value
----              -----
Books             {The kingdoms, Siege, Hamlet}
name              xiaoming
Age
Sex              male

PS c:powershell> $stu. Name= "Qiang Zhao"
PS c:powershell> $stu. Name
Qiang Zhao
PS c:powershell> $stu. Remove ("Name")
PS c:powershell> $stu

Name              Value
----              -----
Books             {The kingdoms, the siege, Hamlet}
age
Sex              male

Format output using a hash table

An interesting application of a hash table in PowerShell can be used to format text output. PowerShell the output of many commands is in tabular form, and of course you can use format-table to customize the table format, for example:

PS c:powershell> Dir | Format-table

  Directory:c:powershell

Mode        lastwritetime   Length Name
----        -------------   ----------
d----    2011/11/23   17:25      ABC
d----    2011/11/29   18:21      MyScript

PS c:powershell> Dir | Format-table fullname,mode

FullName                          Mode
--------                          ----
c:powershellabc                      d----
c:powershellmyscript                   d----
c:powershella.html-a                    ---

The above commands only limit the columns that the table outputs, and hide those columns. But for the width of the column, the column headers are powerless, but with a hash table you can implement more customizations.
Each column of a table contains four properties:
Expression: Bound expression
Width: Column widths
Label: Column headings
Alignment: How columns are aligned

PS c:powershell> $column 1 = @{expression= "Name"; width=30;label= "filename"; alignment= "left"
PS C:powershell > $column 2 = @{expression= "LastWriteTime"; width=40;label= "last Modification" alignment= "right"
PS C: Powershell> ls | Format-table $column 1, $column 2

filename last                       modification
--------                       -----------------
ABC                         2011/11/23 17:25:53
myscript                      2011/11/29 18:21:28
a.html                       2011/11/24 18:30:13

Related Article

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.