Last active
December 1, 2025 22:32
-
-
Save jdhitsolutions/eabe30d34a8fb9dfe8ab71dea9a3eef9 to your computer and use it in GitHub Desktop.
Save and jump to recently used locations in PowerShell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MIT License | |
| Copyright (c) 2025 JDH Information Technology Solutions, Inc. | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in | |
| all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| { | |
| "Index": 1, | |
| "Path": "D:\\", | |
| "Date": "2024-09-16T16:30:56", | |
| "Provider": "FileSystem" | |
| }, | |
| { | |
| "Index": 2, | |
| "Path": "S:\\", | |
| "Date": "2024-09-16T09:46:46", | |
| "Provider": "FileSystem" | |
| }, | |
| { | |
| "Index": 3, | |
| "Path": "S:\\PSScriptTools", | |
| "Date": "2024-11-21T17:33:52", | |
| "Provider": "FileSystem" | |
| }, | |
| { | |
| "Index": 4, | |
| "Path": "C:\\Scripts\\PSIntro", | |
| "Date": "2025-06-16T14:45:30", | |
| "Provider": "FileSystem" | |
| } | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #requires -version 7.5 | |
| #TODO : Turn this into a module | |
| Class SavedLocation { | |
| [Int32]$Index = $global:LocationList.count + 1 | |
| [string]$Path = $($PWD.Path) | |
| [DateTime]$Date = $(Get-Date) | |
| [string]$Provider = (Get-Location).Provider.Name | |
| } | |
| Function Set-LocationFromList { | |
| [cmdletbinding()] | |
| [alias('l') ] | |
| Param([int]$Index = $global:LocationList.Count) | |
| $get = $global:LocationList.Find({ $args.Index -eq $Index }) | |
| if ($get) { | |
| Set-Location -Path $get.Path | |
| } | |
| else { | |
| Write-Warning "Location index $index not found." | |
| } | |
| } | |
| Function Invoke-LocationList { | |
| [cmdletbinding()] | |
| [OutputType('None')] | |
| [alias('ll') ] | |
| Param( | |
| [Parameter(HelpMessage = 'Sort the list by Index, Path or Date.')] | |
| [ValidateSet('Index', 'Path', 'Date')] | |
| [string]$Sort = 'Index', | |
| [Parameter(HelpMessage = 'Show the details of the location list.')] | |
| [switch]$Detail | |
| ) | |
| Write-Host "`n$([char]27)[3;38;5;120mLocation List by $Sort$([char]27)[0m" | |
| if ($Detail) { | |
| $global:LocationList | Sort-Object -Property $Sort | Out-String | Write-Host | |
| } | |
| else { | |
| #$global:LocationList | Select-Object -Property Index, Path | Out-String | Write-Host | |
| $global:LocationList | Sort-Object -Property $Sort | | |
| Format-Table -Property @{Name = 'Index'; Expression = { $_.Index }; Align = 'Right'; Width = 10 }, Path | | |
| Out-String | Write-Host | |
| } | |
| $r = Read-Host "$([char]27)[3;38;5;120mEnter the location index number. Press any other key to cancel$([char]27)[0m" | |
| if ($r -match '\d+') { | |
| Set-LocationFromList -Index $r | |
| } | |
| } | |
| Function Add-SavedLocation { | |
| [cmdletbinding()] | |
| [alias('al')] | |
| Param( | |
| [Parameter(Position = 0)] | |
| [string]$Path = $($PWD.Path) | |
| ) | |
| #add the location to the list if it doesn't exist | |
| if ($LocationList.Find({ $args.path -eq $Path })) { | |
| Write-Verbose 'Location already exists in the list.' | |
| #update the Date property to reflect the last time the location was visited | |
| $LocationList.Find({ $args.path -eq $Path }).Date = $(Get-Date) | |
| } | |
| else { | |
| $LocationList.Add([SavedLocation]::new()) | |
| } | |
| } | |
| Function Clear-LocationList { | |
| [cmdletbinding(SupportsShouldProcess)] | |
| [alias('cll')] | |
| Param() | |
| if ($PSCmdlet.ShouldProcess('$LocationList', 'Clearing location list')) { | |
| $global:LocationList.Clear() | |
| } | |
| } | |
| Function Export-LocationList { | |
| [cmdletbinding(SupportsShouldProcess, DefaultParameterSetName = 'All')] | |
| [alias('ell', 'xll')] | |
| Param( | |
| [Parameter( | |
| Position = 0, | |
| Mandatory, | |
| HelpMessage = 'Enter the file name and path of the JSON file.' | |
| )] | |
| [ValidatePattern('.*\.json')] | |
| [string]$Path, | |
| [Parameter(ParameterSetName = 'All')] | |
| [ArgumentCompleter({ $global:LocationList.Provider | Select-Object -Unique | ForEach-Object { $_ } })] | |
| [string[]]$Provider, | |
| [Parameter( | |
| ParameterSetName = 'All', | |
| HelpMessage = 'Filter the list by location. Regular expressions are supported.' | |
| )] | |
| [string]$Location = '.', | |
| [Parameter(ValueFromPipeline, ParameterSetName = 'InputObject')] | |
| [ValidateNotNullOrEmpty()] | |
| [object]$InputObject, | |
| [Switch]$PassThru | |
| ) | |
| Begin { | |
| Write-Verbose "[$((Get-Date).TimeOfDay) BEGIN ] Starting $($MyInvocation.MyCommand)" | |
| Write-Verbose "[$((Get-Date).TimeOfDay) BEGIN ] Detected parameter set: $($PSCmdlet.ParameterSetName)" | |
| $data = [System.Collections.Generic.List[object]]::new() | |
| } #begin | |
| Process { | |
| if ($InputObject) { | |
| Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Using pipeline input" | |
| $data.Add($InputObject) | |
| } | |
| else { | |
| if ($Provider) { | |
| Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Filtering by provider $($Provider -join '|')" | |
| $data.AddRange([object[]]$($global:LocationList | where Path -Match $Location | where Provider -Match ($Provider -join '|'))) | |
| } | |
| else { | |
| Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Filtering by location $Location" | |
| $data.AddRange([object[]]$($global:LocationList | where Path -Match $Location)) | |
| } | |
| } | |
| } #process | |
| End { | |
| Write-Verbose "[$((Get-Date).TimeOfDay) END ] Exporting $($data.Count) locations to $Path" | |
| #renumber the index because importing resets the location list | |
| $data | ForEach-Object -Begin { $i = 1 } -Process { | |
| Write-Verbose "[$((Get-Date).TimeOfDay) END ] Exporting --> $($_.Path)" | |
| $_.Index = $i++ | |
| } | |
| $data | ConvertTo-Json | Out-File -Path $Path -Encoding utf8 | |
| if ($PassThru -AND (-Not $WhatIfPreference )) { | |
| Get-Item $Path | |
| } | |
| Write-Verbose "[$((Get-Date).TimeOfDay) END ] Ending $($MyInvocation.MyCommand)" | |
| } #end | |
| } | |
| #you can import saved locations from a JSON file | |
| Function Import-LocationList { | |
| [cmdletbinding(SupportsShouldProcess)] | |
| [alias('ill')] | |
| Param( | |
| [Parameter(Position = 0, HelpMessage = 'Enter the file name and path of the JSON file.')] | |
| [ValidateScript({ Test-Path $_ })] | |
| [ValidatePattern('.*\.json')] | |
| [string]$Path = "$PSScriptRoot\locationlist.json" | |
| ) | |
| #clear the existing locationlist | |
| Clear-LocationList | |
| Get-Content -Path $Path | ConvertFrom-Json | ForEach-Object -begin { $i = 1} -process { | |
| # 19 Sept 2024 - validate the path exists before adding it to the list | |
| if (Test-Path -Path $_.Path) { | |
| if ($PSCmdlet.ShouldProcess($_.Path, 'Add location to list')) { | |
| $in = [SavedLocation]::new() | |
| $in.Path = $_.Path | |
| $in.Index = $i | |
| $in.Date = $_.Date | |
| $global:LocationList.Add($in) | |
| $i++ | |
| } #ShouldProcess | |
| } #Test-Path | |
| else { | |
| Write-Information "Path $($_.Path) does not exist. Skipping import." | |
| } | |
| } | |
| } | |
| #initialize the location list and add the current location | |
| If ($global:LocationList.count -eq 0) { | |
| $global:LocationList = [System.Collections.Generic.List[object]]::new() | |
| #add the current location | |
| Add-SavedLocation | |
| } | |
| <# | |
| Demo usage in a PowerShell prompt function | |
| . C:\scripts\locationlist.ps1 | |
| function prompt { | |
| Add-SavedLocation | |
| "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "; | |
| # .Link | |
| # https://go.microsoft.com/fwlink/?LinkID=225750 | |
| # .ExternalHelp System.Management.Automation.dll-help.xml | |
| } | |
| #> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment