字符串,数组,哈希表和对象间的相互转换

哈希表 -> 真正的对象
 
1. [pscustomobject]@{'ID' = 'ZhangSan'; 'Age' = 23}
2. New-Object -TypeName PSObject -Property @{'ID' = 'ZhangSan'; 'Age' = 23}
 
字符串 -> 数组 -> 真正的对象

$str = @"
1 Partridge in a pear tree
2 Turtle Doves
"@
$arr = $list -split "`n"
$arr | foreach {
  $arr0 = ($_ -split ‘ ’, 2)[0]
  $arr1 = ($_ -split ‘ ’, 2)[1]
  [pscustomobject]@{'count'=$arr0;'item'=$arr1}
}

字符串 -> 数组

方法一:
$str = "abcd"
$s2 = $str.GetEnumerator() #$s2是无法使用下标的方式进行索引的,因为其不是array
$s2 | Foreach {$_ + "y8y"}

输出结果:
ay8y
by8y
cy8y
dy8y
 
方法二:
$str = "abcd"
$str.tochararray()
输出结果:
a
b
c
d
 
数组 -> 字符串
方法一:
$info = 'One', 'Two', 'Three'
$info | Out-String
 
方法二:
$info = Get-Content $env:windirwindowsupdate.log -Raw
 
参考: 
http://blog.vichamp.com/powershell/tip/2014/05/19/converting-text-arrays-to-string
http://www.cnblogs.com/dreamer-fish/p/3942023.html
原文地址:https://www.cnblogs.com/IvanChen/p/4493028.html