Set-Home.ps1


Description

Purpose

Changes the current location to the root of the current drive and stores the previous location.

Detailed Description

The Set-Home function pushes the current location onto a stack and then changes the current location to the root of the current drive. Supports navigation back with Restore-Location.

Back to Top

Usage

Example 1

Set-Home

Changes to the root of the current drive and stores the previous location.

Example 2

Set-Home -CustomHome "C:\Users"

Changes to “C:\Users” and stores the previous location.

Back to Top

Notes

Locations are stored in a script-scoped stack for multiple levels of navigation.

Back to Top


Script

# Store the previous locations in a script-scoped stack
$script:locationStack = [System.Collections.Generic.Stack[string]]::new()

function Set-Home {
    <#
    .SYNOPSIS
        Changes the current location to the root of the current drive and stores the previous location.

    .DESCRIPTION
        The Set-Home function pushes the current location onto a stack and then changes the current location to the root of the current drive. Supports navigation back with Restore-Location.

    .PARAMETER CustomHome
        Optional custom path to use as "home" instead of the drive root.

    .EXAMPLE
        Set-Home
        Changes to the root of the current drive and stores the previous location.

    .EXAMPLE
        Set-Home -CustomHome "C:\Users"
        Changes to "C:\Users" and stores the previous location.

    .NOTES
        Locations are stored in a script-scoped stack for multiple levels of navigation.
    #>

    [CmdletBinding(SupportsShouldProcess = $true)]
    param (
        [Parameter(Mandatory = $false, HelpMessage = "Custom path to use as home.")]
        [string]$CustomHome
    )

    try {
        $currentLocation = Get-Location
        Write-Verbose "Storing current location: $currentLocation"

        # Push current location to stack
        $script:locationStack.Push($currentLocation.Path)

        # Determine target location
        if ($CustomHome) {
            $target = $CustomHome
        } else {
            $currentDrive = $currentLocation.Drive.Name
            if (-not $currentDrive) {
                Write-Error "Unable to determine current drive."
                return
            }
            $target = "$currentDrive`:\"
        }

        # Validate target path
        if (-not (Test-Path $target)) {
            Write-Error "Target path '$target' does not exist."
            return
        }

        # Change location with ShouldProcess
        if ($PSCmdlet.ShouldProcess($target, "Change current location")) {
            Set-Location $target
            Write-Verbose "Changed location to: $target"
        }
    }
    catch {
        Write-Error "Failed to change location. Error: $($_.Exception.Message)"
    }
}

Back to Top

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.

Issue


Back to Top