I recently installed `uutils-coreutils` and need to tidy up my PowerShell aliases since some are conflicting. I tried a snippet in my profile script:
coreutils.exe --list | foreach-object {
try {
remove-alias $_ -ErrorAction SilentlyContinue
} catch {}
}
I added `-ErrorAction SilentlyContinue` to bypass errors for non-existent aliases, but it doesn't deal with 'alias is read-only or constant' errors, which is why I included the try-catch block. If I remove `-ErrorAction SilentlyContinue`, I get the nonexistent alias errors popping up. I find it odd that PowerShell requires two different approaches to handle errors. Is my approach correct, or is there a better method to handle this situation?
5 Answers
You can check if `coreutils.exe` exists before attempting to remove aliases. This snippet does just that:
if (Get-Command coreutils.exe -ErrorAction SilentlyContinue) {
$uutils = coreutils.exe --list
Get-Alias -Name $uutils -ErrorAction SilentlyContinue |
Where-Object { $_.Options -notmatch 'Constant' } |
Remove-Alias -Force -ErrorAction SilentlyContinue
}
This way, you bypass read-only aliases effectively without unnecessary errors showing up.
For a simple and quick solution, you can just use:
Get-alias | Remove-Alias -Force
This removes all aliases, so it’s a riskier approach, but it does get the job done fast!
Are you sure you're dealing with aliases here? It sounds like you might be confusing them with actual programs or bat/cmd files.
To resolve conflicts, consider either overriding the binaries with your own aliases or deleting them, or change your `$env:Path`. That could clear things up for you.
Using try/catch with `-ErrorAction SilentlyContinue` isn't very effective because the try/catch handles terminating errors, and `-ErrorAction SilentlyContinue` suppresses them. Try setting the `ErrorActionPreference` to 'Stop' to make every error terminating. Then, your try/catch will catch all errors as you expect.

Wow, this feels liberating! I'm now living on the wild side!