转自: http://simy88.blog.51cto.com/7370552/1683487shell
1、 哈希表用于AD属性修改,-add能够用于新属性的添加,-replace则能够用于属性的修改。express
$custom = @{}数组
$custom.ExtensionAttribute3= 123d
$custom.ExtensionAttribute4 = 'Hello'orm
Set-ADUser -Identity zhangsan -Add $custom对象
2、使用Group-Object快速建立哈希表,Group-Object可以将不少对象其中相同的属性一块儿归为一组;blog
譬如若是咱们要根据文件夹的名称来获取其路径:排序
$FolderHash = Get-ChildItem -Recurse -Path "D:\新建文件夹\桌面\wsusoffline" | where $_.PSIsContainer -eq "false" | select Name,Fullname | Group-Object -AsHashTable -Property nameget
3、哈希表格式化输出来控制列宽it
在powershell的使用过程当中,Format-table是格式化输出很是漂亮的一种方法,虽然其能够用-auto、wrap等参数控制输出格式可是你没法直接控制其宽度仍然是很直观的一种限制,使用哈希表则能够很是巧妙的完成这一功能。
FT表格的每个列包含四个属性:
Expression:绑定的表达式
Width:列宽度
Label:列标题
Alignment:列的对齐方式
$Column1= @{expression="Name"; width=30; label="ServiceName"; alignment="left"}
$Column2= @{expression="DisPlayname"; width=50; label="DisplayName"; alignment="left"}
$Column3= @{expression="Status"; width=30; label="ServiceStatus"; alignment="left"}
Get-Service | Ft $Column1, $Column3, $Column2
4、哈希表的有序使用和顺序更改
哈希表在添加新的对象时会默认将新添加的对象放在最上面,而有序哈希表经过指定类型[Ordered]能够改变这一顺序。
在完成哈希表的成员添加后,若想打破原先的结构而从新插入一个对象,可使用insert的方法来实现。
5、哈希表的嵌套使用
哈希表的嵌套使用可让表的层次结构更清晰,就像多维数组同样。
譬如我要录入某个帐号的属性,其中其地址需细分:
6、给对象和哈希表进行排序
若是要完成主要关键字降序,次要关键字升序的排序:
Dir | Sort-Object@{expression="Length";Descending=$true},@{expression="Name";Ascending=$true}
Creating Custom Tableshttp://technet.microsoft.com/en-us/library/ee692794.aspxWorking with Hash Tableshttp://technet.microsoft.com/en-us/library/ee692803.aspx