PowerShell – Visual Studio Code の利用 No.84

自己投資としてチャレンジしている内容を Blog で公開しています。今回は Ubuntu 上で Visual Studio Code を利用し PowerShell を実行してみたいと思います。(In English PowerShell – Visual Studio Code on Ubuntu No.84)

▼1. Visual Studio Code を利用して PowerShell を実行してみる。

PowerShell は Windows で実行するものと思っていましたが、以下の通り Ubuntu 20.4 や MacOS 11 でも利用可能になりました。

  • Windows Server 2022 with Windows PowerShell 5.1 and PowerShell 7.2
  • Windows Server 2019 with Windows PowerShell 5.1 and PowerShell 7.2
  • macOS 11 with PowerShell Core 7.2
  • Ubuntu 20.04 with PowerShell Core 7.2

PowerShell editing with Visual Studio Code


▼2. 事前準備

2-1. Ubuntu 20.04.2 LTS の利用

https://releases.ubuntu.com/20.04/

2-2. Visual Studio Code のインストール

https://code.visualstudio.com/docs/setup/linux

sudo snap install --classic code

2-3. PowerShell Extension をコマンドでインストール

PowerShell editing with Visual Studio Code

code --install-extension ms-vscode.powershell

2-4. PowerShell をコマンドでインストール

Ubuntu への PowerShell のインストール – PowerShell | Microsoft Learn

// Ubuntu 20.04 の場合

# Update the list of packages
sudo apt-get update
# Install pre-requisite packages.
sudo apt-get install -y wget apt-transport-https software-properties-common
# Download the Microsoft repository GPG keys
wget -q "https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb"
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Update the list of packages after we added packages.microsoft.com
sudo apt-get update
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
pwsh

▼3. PowerShell を VS Code で実行

3-1. ディレクトリの作成

アプリケーションを作成するため、ディレクトリを作成します。

mkidr powershelltest
cd powershelltest

3-2. test.txt 作成

Vi コマンドなどで text.txt を作成し、abcd を記載し保存します。

3-3. Visual Studio を起動

code .

3-4. Powershell の実行

左側に表示されている PowerShell のアイコンをクリックすると PowerShell のターミナル PS > が表示されます。テキストの内容を表示する Get-Content を利用し先ほど作成した test.txt の中身を表示します。

>Get-Content test.txt
abcd
PowerShellOnVSCode

Get-Content (Microsoft.PowerShell.Management) – PowerShell | Microsoft Learn


▼4. 参考情報

  1. VS Code https://code.visualstudio.com/docs/setup/linux
  2. PowerShell Extension PowerShell editing with Visual Studio Code
  3. PowerShell Install Ubuntu への PowerShell のインストール – PowerShell | Microsoft Learn
  4. Get-Content (Microsoft.PowerShell.Management) – PowerShell | Microsoft Learn

以上です。参考になりましたら幸いです。



コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です