LoginSignup
2
2

More than 5 years have passed since last update.

PowerShellで$OLDPWD

Last updated at Posted at 2019-02-18

欲しかったのでなんとかした。ということを書こうとして、本当に標準機能でないのだろうかと調べたところ、Push-LocationPop-Locationで履歴を辿ることはできるらしい。Push-Locationのエイリアスをcdにしておけば解決と言いたかったのだが、こいつらは積んだスタックを順番にしか辿れないのであまり欲しいものではなかった。なので、安心して書き進める。

標準のエイリアスからcdを削除

邪魔なのでまず消す。元はSet-Locationのエイリアス。

rm alias:cd

関数化

エイリアスではどうにもならないので関数にする。

使い始めて気になってた、OldPwdがただの文字列リストで微妙に使い辛い部分に対応した。(2019/2/9 21時頃 追記)

function cd ([parameter(ValueFromPipeline)]$Target) {
  [string]$Path = if ( $Target -is [int] -and $OldPwd[$Target] ) {
    ## 引数が数値で、OldPwdのn番目に何か入ってたら、履歴のn番目のパスを移動先に指定
    $OldPwd[$Target].Path
  } elseif ( $Target -is [string] ) {
    ## 引数が文字列なら、そのままそれを移動先に指定
    $Target
  } elseif ( $Target -is [IO.DirectoryInfo] ) {
    ## 引数がDirectoryInfo型なら、そのフルパスを取得して移動先に指定
    $Target.FullName
  } else {
    ## 全部通り抜けたら、ホームディレクトリを移動先に指定
    $HOME
  }

  ## グローバル変数のOldPwdに現在のディレクトリパスを追加
  [object[]]$global:OldPwd += Get-Location |
    select @{n='Id';e={$global:OldPwd.Count}},
           @{n='Path';e={$_.ProviderPath}}

  ## ディレクトリ移動
  Set-Location $Path
}

動作確認

OldPwdをオブジェクト型に変更したので動作確認結果を全面取り直し。(2019/2/9 21時頃 追記)

文字列の分。

PS> $Pwd.ProviderPath
C:\Windows\system32
PS> cd 'C:\Windows'
PS> $Pwd.ProviderPath
C:\Windows
PS> $OldPwd

Id Path              
-- ----
 0 C:\Windows\system32


PS> 'C:\Program Files' | cd
PS> $Pwd.ProviderPath
C:\Program Files
PS> $OldPwd

Id Path              
-- ----
 0 C:\Windows\system32
 1 C:\Windows

DirectoryInfo型の分。

PS> cd (ls 'C:\Windows' | ? Name -eq 'system32')
PS> $Pwd.ProviderPath
C:\Windows\System32
PS> $OldPwd

Id Path              
-- ----
 0 C:\Windows\system32
 1 C:\Windows
 2 C:\Program Files


PS> ls 'C:\Program Files' | ? Name -eq 'WindowsPowerShell' | cd
PS> $Pwd.ProviderPath
C:\Program Files\WindowsPowerShell
PS> $OldPwd

Id Path              
-- ----
 0 C:\Windows\system32
 1 C:\Windows
 2 C:\Program Files
 3 C:\Windows\System32

ホームディレクトリの分。実際に試したユーザはAdministratorではない。

PS> cd
PS> $Pwd.ProviderPath
C:\Users\Administrator
PS> $OldPwd

Id Path              
-- ----
 0 C:\Windows\system32
 1 C:\Windows
 2 C:\Program Files
 3 C:\Windows\System32
 4 C:\Program Files\WindowsPowerShell

履歴を数値で指定する分。

PS> $OldPwd

Id Path              
-- ----
 0 C:\Windows\system32
 1 C:\Windows
 2 C:\Program Files
 3 C:\Windows\System32
 4 C:\Program Files\WindowsPowerShell
PS> cd 0
PS> $Pwd.ProviderPath
C:\Windows\system32
PS> $OldPwd

Id Path              
-- ----
 0 C:\Windows\system32
 1 C:\Windows
 2 C:\Program Files
 3 C:\Windows\System32
 4 C:\Program Files\WindowsPowerShell
 5 C:\Windows\system32
PS> cd 1
PS> $Pwd.ProviderPath
C:\Windows
PS> $OldPwd

Id Path              
-- ----
 0 C:\Windows\system32
 1 C:\Windows
 2 C:\Program Files
 3 C:\Windows\System32
 4 C:\Program Files\WindowsPowerShell
 5 C:\Windows\system32
 6 C:\Windows

OldPwdもちゃんと全部履歴を持ってくれている。

2
2
2

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
2
2