Search code examples
powershellsessionaliases

Is it possible to create a user defined PowerShell environment?


I would like to clear a PowerShell session of mostly all alias definitions, except for common aliases like cd, sort, mkdir, ...
After I finished my session, I would like to restore all previous known the aliases.

There is no need to unload modules or to unregister CmdLets. I just want to clear the alias namespace for my session.

I could specify the allowed aliases in a list like this:

$AllowedAliases = @(
  "cd", "mkdir", "rm", "rmdir",
  "cd", "mkdir", "rm", "rmdir",
  "where", "select",
  "sort"
)

How can I save the aliases and restore them?
or
How can I start a clean PoSh and load only basic aliases?


What I have tested so far:

The following lines are from my example module called poc.psm1.

$Aliases = @()

function Register-PoC
{ foreach ($a in (Get-Item Alias:))
  { $script:Aliases += $a
    Write-Host "$($a.Name) => $($a.ReferencedCommand) ($($a.Visibility))"
    Remove-Item "Alias:$($a.Name)" -Force
  }
}

function Unregister-PoC
{ foreach ($a in $script:Aliases)
  { Write-Host "$($a.Name) <= $($a.ReferencedCommand)"
    if (Test-Path "Alias:$($a.Name)")
    { Write-Host "$($a.Name) exists."      }
    else
    { Set-Alias -Name $a.Name -Value $a.ReferencedCommand -Scope $a.Visibility    }
  }

  if (Test-Path Alias:quit)   {  Remove-Item Alias:quit  }
  Remove-Module PoC
}

Export-ModuleMember -Function 'Register-PoC'
Export-ModuleMember -Function 'Unregister-PoC'

Register-PoC

Set-Alias -Name quit -Value Unregister-PoC     -Description "Unload this module." -Scope Global

Usage example:

Import-Module .\poc.psm1
dir Alias:
quit
dir Alias:

Unfortunately, dir Alias: is not empty after invoking my script...

Another thing is, that I should preserve some settings of these aliases, because manual test showed, that dir does not behave like dir in before:

Remove-Item dir
Set-Alias dir Get-Item
dir
Cmdlet Get-Item an der Befehlspipelineposition 1
Geben Sie Werte für die folgenden Parameter an:
Path[0]:

So dir seams to append a default path to Get-Item if non is set to the alias.


Solution

  • Aliases are scoped. When you remove all aliases within a function, the aliases in the global scope aren't affected. Here is code that worked for me (simplifies your code a bit, although I didn't touch unregister-PoC, which could also be simplified I think):

    function Register-PoC {
      $script:aliases = get-item alias:
      remove-item alias:* -force
    }
    
    function Unregister-PoC
    { foreach ($a in $script:Aliases)
      { Write-Host "$($a.Name) <= $($a.ReferencedCommand)"
        if (Test-Path "Alias:$($a.Name)")
        { Write-Host "$($a.Name) exists."      }
        else
        { Set-Alias -Name $a.Name -Value $a.ReferencedCommand -Scope $a.Visibility    }
      }
    
      if (Test-Path Alias:quit)   {  Remove-Item Alias:quit  }
      Remove-Module PoC
    }
    
    . Register-PoC
    
    Set-Alias -Name quit -Value Unregister-PoC     -Description "Unload this module." -Scope Global
    

    Note the dot operator on Register-PoC. You will need to dot source quit to restore the aliases to global scope.

    BTW, rather than the foreach loop in Unregister-PoC you could use copy-item.