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?

PowerShellにエイリアスを設定してWSLの起動を楽にする

Last updated at Posted at 2025-01-31

はじめに

WindowsでPython開発をしているとWSLのコマンドを一々入力するのが面倒になってきました。

ということでPowerShellのエイリアスにコマンドを登録してみます。

やりたいこと

  • PowerShellにコマンドのエイリアスを追加してスムーズにWSL環境へ接続する

エイリアスに登録するWSLコマンド

WSLで構築した環境の一覧

PS C:\Users\Nichi\work> wsl -l -v
  NAME            STATE           VERSION
* Ubuntu-24.04    Stopped         2

イメージの起動

PS C:\Users\Nichi\work> wsl -d Ubuntu-24.04
nichi@Priv-001:/mnt/c/Users/Nichi/work$

準備

プロファイルの確認

Test-Path $profile

プロファイルの作成(確認がTrueなら不要)

New-Item -Path $profile -Type File -Force

プロファイルの編集

notepad $PROFILE

開いたメモ帳に関数とエイリアスを書き込んでいきます。

何故関数を使っているかというと、Powershellでは引数ありコマンドのエイリアスは定義できないためです。

function wsl-list {
    wsl -l -v
}
Set-alias -Name vl -Value wsl-list

function wsl-up{
    wsl -d Ubuntu-24.04
}
Set-alias -Name vr -Value wsl-up

プロファイルの再読み込み

. $PROFILE

実行

とても楽

PS C:\Users\Nichi\work> vl
  NAME            STATE           VERSION
* Ubuntu-24.04    Stopped         2
PS C:\Users\Nichi\work> vr
nichi@Priv-001:/mnt/c/Users/Nichi/work$ 
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?