Powershellのバージョンの確認方法

コンピュータ

はじめに

Powershellスクリプトがバージョン違いにより動かない場合、まずPowershellスクリプトを実行している環境のバージョンを知る必要があります。

Windowsごとのデフォルトバージョン

Windows7
2.0
Windows8
3.0
Windows8.1
4.0
Windows10
5.0

PowerShellスクリプトを初めて実行する場合

PowerShellスクリプトの実行を許可してください。
Powershellのps1ファイルを実行する方法
PowersehllをはじめるにあたりWindowsのコマンドラインインターフェースであるPowershellはコマンドレットと言われる命令をテキストファイルに記述したスクリプトファイルを実行することが出来ます。Powershellは...

Powershellの起動方法

以下の記事を参照
Powershellのps1ファイルを実行する方法
PowersehllをはじめるにあたりWindowsのコマンドラインインターフェースであるPowershellはコマンドレットと言われる命令をテキストファイルに記述したスクリプトファイルを実行することが出来ます。Powershellは...

$PSVersionTableを参照する

Powershell上で以下のコマンドを実行する。

$PSVersionTable

結果の「$PSVersionTable」の値がPowershellのバージョンです。

PowerShellのバージョンにより処理を分岐するサンプルプログラム

メジャーバージョンが5以下の場合powershell.exe5より大きい場合pwsh.exeのパス文字列を返す。

$psExePath = if ($PSVersionTable.PSVersion.Major -le 5)
{
    echo "${PSHOME}`\powershell.exe"
} else {
    echo "${PSHOME}`\pwsh.exe"
}
Write-Host $psExePath

Windows7にPowershell Version5.1をインストール

以下のサイトからインストーラーをダウンロード
Download Windows Management Framework 5.1 from Official Microsoft Download Center
Windows Management Framework 5.1 includes updates to Windows PowerShell, Windows PowerShell Desired State Configuration (DSC), Windows Remote Management (WinRM)...

Win7AndW2K8R2-KB3191566-x64.zip(64bit版)
Win7-KB3191566-x86.zip(32bit版)
ダウンロードしたzipファイルを展開します。
「Install-WMF5.1.ps1」が更新スクリプトですので、実行します。

更新が完了すると再起動を要求されます。

コメント