tech.guitarrapc.cóm

Technical updates

PowerShell の ps1 ファイルは 何故 1なのか

良くある疑問だと思いますが日本語での説明が見当たらないので、 Twitter で説明していましたが記事にしておきます。

 

理由

参考

PowerShell MVPの方々が説明しています。 引用しましょう。

Why ".PS1" file extension for PS scripts? It does stand for 1. But when they started work on version 2, they decided that instead of having it install side-by-side with v1, they would try to make it "compatible" with 1. So instead of requiring you to have both PowerShells installed to run .ps1 and .ps2 scripts, they would make all ps v1 scripts work in ps v2... Of course, there's a TON of features in v2 that don't work in v1, but for some reason they abandoned the file extension as a way of differentiating it, and chose to use a comment: #requires -version 2.0 If you ask me, considering how many language features and cmdlets are not backwards compatible, it's a mistake -- they should use .ps2 and just allow it to run .ps1 *also* -- but they've decided to save the "breaking" of changing file extensions for a hypothetical future version which might be language incompatible. -- Joel

 

現状の .ps1 を使うことにした 大元の解説は PowerShell v2.0 発表時に説明されています。 こちらも引用しておきます。

PowerShell Scripts continue to use ".PS1". CTP: Versioning PowerShell Scripts continue to use ".PS1". We will continue to pursue this approach until there is a major change in the CLR or .NET frameworks which force us to go side-by-side. It is at that point that we'll go from .PS1 to .PS2 . Until then we will stay with .PS1 and everything that runs today will continue to run in new releases. You might write a .PS1 script which takes advantage of a cmdlet/feature that is only available in V2. If you send this to someone that has PS V1, it will fail "in some way and at some time". If it uses some new syntax, it will fail at parse time (with a PARSER error) but if it uses a new command line switch - it won't fail until it tries to run that command. This is what #REQUIRES is all about. You start your script with #REQUIRES -Version 2 And we will check version #'s and produce a precise error message If you have a #REQUIRES –VERSION 1 in your script, it will continue to run just find on PowerShell (V2) CTP because it is compatible with V1.

 

まぁ、PowerShell 4.0 でも まだまだ ps1 です。