Last active
December 5, 2020 18:24
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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