Restore-Location.ps1
19 Sep 2025Description
Purpose
Changes the current location back to the previously stored location.
Detailed Description
The Restore-Location function pops the last stored location from the stack and changes the current location to it.
Usage
Example 1
Restore-Location
Changes back to the last stored location.
Notes
Uses a script-scoped stack to manage location history.
Script
# Store the previous locations in a script-scoped stack
$script:locationStack = [System.Collections.Generic.Stack[string]]::new()
function Restore-Location {
<#
.SYNOPSIS
Changes the current location back to the previously stored location.
.DESCRIPTION
The Restore-Location function pops the last stored location from the stack and changes the current location to it.
.EXAMPLE
Restore-Location
Changes back to the last stored location.
.NOTES
Uses a script-scoped stack to manage location history.
#>
[CmdletBinding()]
param ()
try {
if ($script:locationStack.Count -eq 0) {
Write-Warning "No previous location stored."
return
}
$previousLocation = $script:locationStack.Pop()
Write-Verbose "Retrieving previous location: $previousLocation"
# Validate previous location
if (-not (Test-Path $previousLocation)) {
Write-Warning "Previous location '$previousLocation' no longer exists. Removing from stack."
return
}
# Change location
Set-Location $previousLocation
Write-Verbose "Changed location back to: $previousLocation"
}
catch {
Write-Error "Failed to change location back. Error: $($_.Exception.Message)"
}
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.