-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathChange-Directory.ps1
More file actions
46 lines (39 loc) · 1.07 KB
/
Change-Directory.ps1
File metadata and controls
46 lines (39 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
param($cmd, $ShowCount = 10)
function Internal-Change-Directory($cmd, $ShowCount){
function Get-CommandList() {
(Get-Location -Stack).ToArray() | Select-Object Path -Unique
}
function Print-Extended-CD-Menu(){
$index = 1;
foreach($location in Get-CommandList){
Write-Host ("{0,6}) {1}" -f $index, $location.Path)
$index++
if($index -gt $ShowCount){
break;
}
}
}
switch($cmd) {
"" { Print-Extended-CD-Menu }
"?" { Print-Extended-CD-Menu }
default {
$newLocation = $cmd;
# check to see if we're using a number command and get the correct directory.
[int]$cdIndex = 0;
if([system.int32]::TryParse($cmd, [ref]$cdIndex)) {
$results = (Get-CommandList);
if( ($results | measure).Count -eq 1 ){
$newLocation = $results.Path
}
else {
$newLocation = (Get-CommandList)[$cdIndex-1].Path
}
}
#If we are actually changing the dir.
if($pwd.Path -ne $newLocation){
Push-Location $newLocation
}
}
}
}
Internal-Change-Directory -cmd $cmd -ShowCount $ShowCount