Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.