Skip to content

Instantly share code, notes, and snippets.

@bpatra
Last active December 5, 2020 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpatra/6159032 to your computer and use it in GitHub Desktop.
Save bpatra/6159032 to your computer and use it in GitHub Desktop.
Part of a PoSh profile to import/export all executables located under PATH.Export-AliasWithEXEInPATH is a function that exports alias with all .exe located under PATH environment variable. The alias exported file is named alias.txt and is exported next to the $PROFILE .ps1 file. These aliases are imported just before loading module PowerTab.
$aliasesPath = Join-Path -Path ([System.IO.Path]::GetDirectoryName($PROFILE)) -ChildPath "alias.txt"
function Export-AliasWithEXEInPATH
{
$aliasDict = @{}
$existingAlias = Get-Alias | ForEach-Object {$_.Name}
foreach($aliasName in $existingAlias)
{
$aliasDict.Add($aliasName,"")
}
$pathDirectories = $env:PATH.Split(';') | Where-Object {($_ -ne "") -and (Test-Path $_ )}
foreach($pathDir in $pathDirectories)
{
$result = Get-ChildItem (Join-Path $pathDir "*") -Include *.exe |
ForEach-Object {
Write-Host "Checking" $_.BaseName "for aliasing"
If(-Not $aliasDict.ContainsKey($_.BaseName))
{
$alias = $_.BaseName.ToString()
$for = $_.Name
Set-Alias -Name $alias -Value $for -Verbose -Scope "Global"
}
}
}
Export-Alias $aliasesPath
}
if(-Not (Test-Path $aliasesPath))
{
Write-Error "Cannot find alias.txt execute function Export-AliasWithEXEInPATH"
}
else
{
Import-Alias $aliasesPath -Force
}
Import-Module "PowerTab" -ArgumentList "C:\Users\benoit\Documents\WindowsPowerShell\PowerTabConfig.xml"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment