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?

More than 3 years have passed since last update.

Windows10でPowershell 2を利用する方法【バージョン確認方法】

Posted at

0. はじめに

PowerShellスクリプトは非常に便利なため、業務で利用されている方もいるかと思います。
ただ、開発環境で利用しているPCはwindows 10などだが、実際に実装する環境はWindows Serverで、PowerShell 2が実装されているということもあるかと思います。
今回は開発環境のPowerShellバージョンを2に変更する方法を説明します。

1. バージョン確認方法

最初にバージョン確認方法を紹介します。
以下のコマンドで参照できます。

$PSVersionTable

私の開発環境(Windows 10)で実行すると以下のようになります。

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.1320
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1320
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

また、バージョン情報だけ表示する場合は以下のようにします。

PS C:\> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      19041  1320

2. バージョン変更方法

PowerShell 2に変更するには、以下のコマンドを実行します。

powershell -v 2

実行し、$PSVersionTableで確認すると変更されていることがわかります。

PS C:\> powershell -v 2
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\> $PSVersionTable

Name                           Value
----                           -----
CLRVersion                     2.0.50727.9151
BuildVersion                   6.1.7600.16385
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1

PS C:\> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1

Compress-Archiveコマンドレットで確認

例えばPowershell 5(4からかもしれません)からはCompress-Archiveというファイルをzip圧縮するコマンドレットが追加されました。これをpowershel実行しようとすると、以下のようにエラーとなります。
開発環境からでも、実際に実装する環境で動かせるか確認するのに便利ですね。

PS C:\> powershell -v 2
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\> Compress-Archive
Compress-Archive : 用語 'Compress-Archive' は、コマンドレット、関数、スクリプト ファイル、または操作可
能なプログラムの名前として認識されません。名前が正しく記述されていることを確認し、パスが含まれている場
合はそのパスが正しいことを確認してから、再試行してください。
発生場所 行:1 文字:17
+ Compress-Archive <<<<
    + CategoryInfo          : ObjectNotFound: (Compress-Archive:String) []、CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
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?