●返り値の型について
PS C:\Users\customer\Desktop\test> dir
ディレクトリ: C:\Users\customer\Desktop\test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2008/04/08 2:09 783 FileConvert.ps1
-a---- 2017/01/14 21:07 6 SJIS.txt
-a---- 2017/01/14 21:07 8 UTF.txt
# ヒット数が2個以上なので、Object[]が戻り値になる
PS C:\Users\customer\Desktop\test> (Get-ChildItem . -Exclude "*.ps1" -name).GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
# ヒット数が1個なので、配列が戻り値となっていない
PS C:\Users\customer\Desktop\test> (Get-ChildItem . -Exclude "*.txt" -name).GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
# 強制的に配列で返したい時は、@を文(式?)の前につける
PS C:\Users\customer\Desktop\test> @(Get-ChildItem . -Exclude "*.txt" -name).GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
●文字エンコーディングを統一したい
$file_count = (Get-ChildItem . -Exclude "*.ps1" -name).count
$a = Get-ChildItem . -Exclude "*.ps1" -name
for ($i = 0; $i -lt $file_count; $i++){
Get-Content $a[$i]
}
あ # 「SJIS.txt」はShift-JISで保存しているので文字化けせず表示される
あ
縺・ # 「UTF.txt」UTF-8で保存しているので文字化け
縺