ラベル PowerShell の投稿を表示しています。 すべての投稿を表示
ラベル PowerShell の投稿を表示しています。 すべての投稿を表示

2025年6月4日水曜日

スクリプトのテキストファイル形式

ANSIかUTF-8(BOM付き)のどちらか。

Unix系のではテキストファイルはUTF-8(BOM付き)は正しく処理されない。

Visual Studio CodeではテキストファイルはUTF8NoBOMで保存される。Ascii文字以外の文字が含まれると文字がPowerShellは正しく処理されない。

結論として、 

PowerShellスクリプトでAscii文字以外を使用するには、UTF-8(BOM付き)にする必要がある。PowerShellスクリプトファイルはBOMがないファイルはANSIとして処理される。

 

https://learn.microsoft.com/ja-jp/powershell/module/microsoft.powershell.core/about/about_character_encoding?view=powershell-7.5

画面出力

Write-Host 非推奨

Write-Verbose ユーザー側でVerbose パラメーターで出力を指定できる。

Write-Output 出力される。 

https://learn.microsoft.com/ja-jp/powershell/utility-modules/psscriptanalyzer/rules/avoidusingwritehost?view=ps-modules

文字列の分割

構文

-split <String>
-split (<String[]>)
<String> -split <Delimiter>[,<Max-substrings>[,"<Options>"]]
<String> -split {<ScriptBlock>} [,<Max-substrings>]
Max-substrings

PowerShell 7以降:マイナス指定で末尾から数えて処理される。

 

https://learn.microsoft.com/ja-jp/powershell/module/microsoft.powershell.core/about/about_split?view=powershell-7.5