[CmdletBinding(SupportsShouldProcess)]param() function RemoveAlias { param([string]$Name) $local:a = $Name | Where-Object { $_ } | Get-Alias -ErrorAction SilentlyContinue if ( -not $a ) { return } if ( Get-Command -ListImported Remove-Alias -ErrorAction SilentlyContinue ) { $a | Remove-Alias -Scope $MyAliasScope -ErrorAction SilentlyContinue } else { Remove-Item "Alias:$a" } } function SafeSetAlias { param([string]$Name, [string]$Value, [string]$Description) . RemoveAlias $Name try { Set-Alias $Name $Value -Scope $MyAliasScope -Description:$Description -ErrorAction Stop } catch { Set-Alias $Name $Value -Scope $MyAliasScope -Description:$Description -Option AllScope } } try { Get-Alias Get-MyAliases -ErrorAction Stop | ForEach-Object { . RemoveAlias $($_.Name) } } catch {} function Get-MyAliases { [CmdletBinding()]param([switch]$ScriptsOnly) $local:allAliases = @() $MyAliasScope = 0 $local:_scope = 0 $local:_done = $false do { try { $local:newAliases += Get-Alias -Scope $_scope | Where-Object { ($_.Definition -like "$MyPSScriptRoot*") -or (-not $ScriptsOnly -and ($_.Description -match '#MyAlias')) } if ( $newAliases ) { $allAliases += $newAliases $MyAliasScope = $_scope; Write-Verbose "`$MyAliasScope is now set to $MyAliasScope" } } catch { Write-Verbose "catch: $($_.Exception.Message)" $_done = $_.Exception.Message -match 'The scope .* exceeds' } $_scope += 1 } until ( $_done ) $allAliases } function getScriptName { param([string]$FullPath) $FullPath -replace '\.ps1$', '' -replace "^$([regex]::Escape($MyPSScriptRoot)).", '' } $local:IsVerbose = [bool]($PSBoundParameters['Verbose']) # Loads mandatory Package code try { $null = [Packagesz] } catch { $local:PackagePath = $(Join-Path $MyPSScriptRoot 'base') Join-Path $(Join-Path $PackagePath profile.d) 'classes.ps1' | Where-Object { Test-Path $_ } | ForEach-Object { . $_ } } $local:myPackages = Get-MyPackages -ReturnFullPath -IncludeRoot | Get-Item $local:myAliases = [ordered]@{} $myPackages | ForEach-Object { Join-Path $_.FullName Aliases } | Where-Object { Test-Path $_ } | Get-ChildItem | ForEach-Object { $myAliases[$_.BaseName] = Get-Content $_.FullName } $script:MyAliasScope = 0 $local:oldAliases = Get-MyAliases $oldAliases = Get-Alias -Scope $MyAliasScope | Where-Object Name -in $($oldAliases.Name + $myAliases.Keys) if ( $oldAliases -and $IsVerbose ) { Write-Verbose "Removing: $($oldAliases.Name -join ', ')" } $oldAliases | ForEach-Object { . RemoveAlias $($_.Name) } if ( $(. Get-ScopeDepth) -gt 0 ) { Write-Host -ForegroundColor Red "Try sourcing Reload-MyScripts instead of just running it" } foreach ( $local:PackagePath in $myPackages ) { #$local:PackageName = $PackagePath.Name Join-Path $PackagePath 'profile.d' | Where-Object { Test-Path $_ } | Get-ChildItem -Filter '*.ps1' | ForEach-Object { Write-Verbose "Loading $(getScriptName $_.FullName)..."; ". '$($_.FullName)';" } | Invoke-Expression $local:CommandsToAlias = ( @( $PackagePath ) + $( [SystemName]::_GetValidValues("", $true, $true) | ForEach-Object { Join-Path $PackagePath "sys.$_" } )) | Where-Object { Test-Path $_ } | ForEach-Object { Get-ChildItem (Join-Path $_ '*.ps1') | Where-Object Name -notmatch '\.inc\.ps1$' } foreach ( $local:newAlias in $CommandsToAlias ) { Write-Verbose "Creating alias for $(getScriptName $newAlias.FullName) Script..." . SafeSetAlias $($newAlias.BaseName) $($newAlias.FullName) } } foreach ( $local:alias in $myAliases.Keys ) { Write-Verbose "Adding $($alias) alias..." . SafeSetAlias $alias $myAliases[$alias] '#MyAlias' } Remove-Item Function:\RemoveAlias, Function:\SafeSetAlias -ErrorAction SilentlyContinue