SHARE
TWEET

Powershell Directory Stack Minipulation

cwprogram Jul 11th, 2011 504 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function List-Stack {
  2.     $index = 0;
  3.     $locations = (Get-Location -Stack).ToArray()
  4.     foreach($location in $locations)
  5.     {
  6.         Write-Host $index $location.Path
  7.         $index++
  8.     }
  9. }
  10.  
  11. function Set-NewLocation($index) {
  12.     $location = (Get-Location -Stack).ToArray() | select -Index $index
  13.     Set-Location -Path $location
  14. }
  15.  
  16. function Clear-Stack {
  17.     $locations = (Get-Location -Stack).ToArray()
  18.     foreach($location in $locations)
  19.     {
  20.         Pop-Location
  21.     }
  22. }
  23.  
  24. # Aliases for less typing, change as you see fit
  25. Set-Alias -name 'cwd' -value 'Push-Location'
  26. Set-Alias -name 'cds' -value 'Set-NewLocation'
  27. Set-Alias -name 'cdl' -value 'List-Stack'
  28.  
  29. Clear-Stack
  30.  
  31. cwd c:\Windows\System32
  32. cwd c:\Windows
  33. cwd c:\Temp
  34. # This will not be in the stack unless we cwd somewhere else
  35. cwd c:\Users
  36.  
  37. cdl
  38.  
  39. cds 2
  40.  
  41. <# Output:
  42. PS C:\users\Chris> C:\Users\Chris\Documents\dirtest.ps1
  43.  
  44. 0 C:\Temp
  45. 1 C:\Windows
  46. 2 C:\Windows\System32
  47. 3 C:\users\Chris
  48.  
  49. PS C:\Windows\System32>
  50. #>
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top