Windows Powershell使用雜湊表_PowerShell

來源:互聯網
上載者:User

雜湊表存放的是對,在雜湊表中不再僅僅限制使用數字定址,可以使用任意類型的資料類型定址。

建立雜湊表
之前使用@()建立數組,現在使用@{}建立雜湊表,使用雜湊表的鍵訪問對應的值。

PS C:Powershell> $stu=@{ Name = "小明";Age="12";sex="男" }PS C:Powershell> $stuName              Value----              -----Name              小明Age              12sex              男PS C:Powershell> $stu["Name"]小明PS C:Powershell> $stu["age"]12PS C:Powershell> $stu.Count3PS C:Powershell> $stu.KeysNameAgesexPS C:Powershell> $stu.Values小明12男

在雜湊表中儲存數組

可以在建立雜湊表時就使用數組,因為建立數組和雜湊表的的元素關鍵字不衝突。一個是逗號,一個是分號。

PS C:Powershell> $stu=@{ Name = "小明";Age="12";sex="男";Books="三國演義","圍城","哈姆雷特" }PS C:Powershell> $stuName              Value----              -----Books             {三國演義, 圍城, 哈姆雷特}Name              小明Age              12sex              男

在雜湊表中插入新的索引值

在雜湊表中插入新的索引值很方便,象定義變數一樣,可以直接拿來使用

PS C:Powershell> $Student=@{}PS C:Powershell> $Student.Name="令狐沖"PS C:Powershell> $Student.School="華山派"PS C:Powershell> $StudentName              Value----              -----Name              令狐沖School             華山派

雜湊表值的更新和刪除

如果要更新鍵的值,可以直接重寫。如果要刪除這個索引值對,可以使用Remove方法,參數為Key

PS C:Powershell> $stuName              Value----              -----Books             {三國演義, 圍城, 哈姆雷特}Name              小明Age              12sex              男PS C:Powershell> $stu.Name="趙強"PS C:Powershell> $stu.Name趙強PS C:Powershell> $stu.Remove("Name")PS C:Powershell> $stuName              Value----              -----Books             {三國演義, 圍城, 哈姆雷特}Age              12sex              男

使用雜湊表格式化輸出

在Powershell中雜湊表的一個有趣的應用可以用來格式化文本輸出。Powershell許多命令的輸出結果都是以表格的形式,當然可以使用Format-Table自訂表格格格式,例如:

PS C:Powershell> Dir | Format-Table  Directory: C:PowershellMode        LastWriteTime   Length Name----        -------------   ------ ----d----    2011/11/23   17:25      ABCd----    2011/11/29   18:21      myscriptPS C:Powershell> Dir | Format-Table FullName,ModeFullName                          Mode--------                          ----C:PowershellABC                      d----C:Powershellmyscript                   d----C:Powershella.html                    -a---

上述的命令只能限制表格輸出那些列,隱藏那些列。但是對於列的寬度,欄位標題無能為力,但是有了雜湊表就可以實現更多自訂了。
表格的每一個列包含四個屬性:
Expression:綁定的運算式
Width:列寬度
Label:欄位標題
Alignment:列的對齊

PS C:Powershell> $column1 = @{expression="Name"; width=30;label="filename"; alignment="left"}PS C:Powershell> $column2 = @{expression="LastWriteTime"; width=40;label="last modification"; alignment="right"}PS C:Powershell> ls | Format-Table $column1, $column2filename                       last modification--------                       -----------------ABC                         2011/11/23 17:25:53myscript                      2011/11/29 18:21:28a.html                       2011/11/24 18:30:13

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.