PSDirectoryStack.psm1
# Define a global stack variable if (-not (Get-Variable -Name DirectoryStack -Scope Global -ErrorAction SilentlyContinue)) { Set-Variable -Name DirectoryStack -Value @() -Scope Global } function Push-Location { param( [string]$Path ) # Save the current location $global:DirectoryStack += (Get-Location) if ($Path) { Set-Location -Path $Path } } function Pop-Location { if ($global:DirectoryStack.Count -eq 0) { Write-Host "Directory stack is empty." return } # Retrieve the last location $lastLocation = $global:DirectoryStack[-1] # Remove the last location from the stack $global:DirectoryStack = $global:DirectoryStack[0..($global:DirectoryStack.Count - 2)] # Change to the last location Set-Location -Path $lastLocation } function Switch-Location { if ($global:DirectoryStack.Count -eq 0) { Write-Host "Directory stack is empty." return } # Retrieve the last location $lastLocation = $global:DirectoryStack[-1] # Remove the last location from the stack $global:DirectoryStack = $global:DirectoryStack[0..($global:DirectoryStack.Count - 2)] # Save the current location $global:DirectoryStack += (Get-Location) # Change to the last location Set-Location -Path $lastLocation } function Update-UserProfileForEnhancedLocationModule { param ( [Alias('h')] [switch]$Help ) # Manually check for --help argument in the invocation line $invocation = $PSCmdlet.MyInvocation if ($Help -or $invocation.Line -like "*--help*") { @" NAME Update-UserProfileForEnhancedLocationModule SYNOPSIS Updates the user's PowerShell profile to import the EnhancedLocationModule and set aliases. SYNTAX Update-UserProfileForEnhancedLocationModule [-Help] DESCRIPTION This function dynamically determines the path to the EnhancedLocationModule's .psm1 file, updates the user's PowerShell profile to import the module, and sets aliases for enhanced location handling. It then reloads the profile to apply the changes immediately. PARAMETERS -Help Displays this help message. EXAMPLES PS> Update-UserProfileForEnhancedLocationModule Updates the user's profile with the module import and alias commands, then reloads the profile. NOTES Author: T. Blackstone Company: Inspyre-Softworks This function is part of the PSDirectoryStack module. "@ return } # Determine the path of the currently executing script/module $modulePath = (Get-Module -ListAvailable -Name PSDirectoryStack).ModuleBase Write-Debug "Found module directory: $modulePath" if (-not $modulePath) { Write-Error "Unable to determine the module path. Make sure the script is executed from the correct location." return } # Construct the path to the .psm1 file in the same directory $modulePsm1Path = Join-Path -Path $modulePath -ChildPath "PSDirectoryStack.psm1" if (-not (Test-Path -Path $modulePsm1Path)) { Write-Error "The module file PSDirectoryStack.psm1 was not found in the expected location: $modulePsm1Path" return } # Get the path to the user's profile script $profilePath = $PROFILE # Content to add to the profile script $profileContentToAdd = @" # Import the EnhancedLocationModule module Import-Module -Name '$modulePsm1Path' # Create an alias for 'cd' to use Set-LocationEnhanced Set-Alias -Name cd -Value Set-LocationEnhanced -Option AllScope # Optional: Create aliases for Push-Location and Pop-Location Set-Alias -Name pushd -Value Push-Location -Option AllScope Set-Alias -Name popd -Value Pop-Location -Option AllScope Set-Alias -Name switchd -Value Switch-Location -Option AllScope "@ # Check if the profile already contains the necessary content $profileContent = Get-Content -Path $profilePath -Raw if ($profileContent.Contains($profileContentToAdd.Trim())) { Write-Host "Profile already contains the necessary imports and aliases." return } # Append the content to the profile script Add-Content -Path $profilePath -Value $profileContentToAdd Write-Host "Profile updated successfully. Reloading profile..." # Reload the profile script . $profilePath Write-Host "Profile reloaded successfully." } # Example usage: # Update-UserProfileForEnhancedLocationModule |