@applejxd

雑記帳

Powershell で Symlink

目次

1. シンボリックリンクの作成方法

PowerShell 標準コマンド

PowerShell 5.0 以降:

# ファイル
New-Item -ItemType SymbolicLink -Path "リンク名.txt" -Target "C:\path\to\実ファイル.txt"

# ディレクトリ
New-Item -ItemType SymbolicLink -Path "リンクフォルダ" -Target "C:\path\to\実フォルダ"

cmd.exe の内部コマンドを呼び出す方法:

# ファイル
cmd /c mklink "リンク名.txt" "C:\path\to\実ファイル.txt"

# ディレクトリ
cmd /c mklink /D "リンクフォルダ" "C:\path\to\実フォルダ"

2. Junction との比較

種類 コマンド例 (PowerShell) 主な用途 特徴
SymbolicLink New-Item -ItemType SymbolicLink -Path "C:\Users\foo\.config" -Target "D:\dotfiles\config" 最新環境推奨 - Vista以降
- NTFS必須
- ネットワークドライブも可
- 管理者権限 or 開発者モード必須
- Linuxのsymlinkと同等
Junction New-Item -ItemType Junction -Path "C:\Users\foo\.config" -Target "D:\dotfiles\config" レガシー互換・安定 - 2000以降
- NTFS必須
- ディレクトリ専用
- 管理者権限不要
- ネットワークドライブ不可

3. 選び方


4. dotfiles 用途の例

chezmoi 管理で $HOME\.config を差し替える場合:

New-Item -ItemType SymbolicLink -Path "$HOME\.config\fish" -Target "D:\dotfiles\.config\fish"

Junction

New-Item -ItemType Junction -Path "$HOME\.config\fish" -Target "D:\dotfiles\.config\fish"

5. 結論