0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VsCodeで作成したWindows Powershellスクリプトが動かないとき

Last updated at Posted at 2025-01-07

おさらい

  • 忙しい人向け

Windows PowerShell と PowerShell は、2 つの個別の製品です。

要はURLとURIみたいなもん、細けえことは良いんだよ

なぜ動かないかはアタリはついていた

image.png
これ文字化けしてる可能性大という話で、結局原因もそうだったというお話。

Windows PowerShell ISE では、ASCII、BigEndianUnicode、Unicode、UTF32、UTF7、UTF8、既定値のエンコード オプションをサポートしています。 既定値オプションの値は、システムによって異なります。

まぁ大方の人はASCIIになってんじゃないの?

解決策

  • Shift-JISで保存する
    なんとなく文字コードの問題だなと思って試してみたら動いた。個人的にはShift-JISとCP932さんという存在には、過去にも困らされていたし、滅べばいいと思っているので、別の手法を採用した。けどcodeでエンコード変えて保存するだけだし影響範囲的にはこちらのほうが良かったのではないかと思っている。

  • UTF-8 BOM付きでよみこむようにプロファイルファイルを新規追加
powershell MakeProfile.ps1
new-item -path $profile -itemtype file -force
notepad $profile

出てきたメモ帳に以下を記載

powershell Microsoft.PowerShell_profile.ps1
$OutputEncoding = [System.Text.Encoding]::GetEncoding('utf-8')

image.png

脳死でANSIとか言う聞いたこと無い文字コードで保存。

その後走らせたいPS1ファイルをUTF-8 with BOMで保存

これで日本語を含んだPS1を右クリック>Powershellで実行で無事に実行できるようになる。

書いてて思ったこと

Shift-jisで保存の解決法がベストプラクティスな感じがしてきた。
シンプルであり、影響範囲はそのファイルのみ。
今後運用してみてなんか問題があれば加筆か別記事を書きます。

もとに戻す

UTF-8 with BOMは影響範囲でかすぎぃ!

powershell RemoveProfile.ps1
Remove-Item -Path $profile -Force

なんかclearでも元に戻らなかったのでPowershellを再起動

powershell ChkEncode.ps1
$OutputEncoding.EncodingName

image.png

もとに戻った。日本語含むPS1ファイルはS-JISで運用することにした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?