Powershell alias inside function not available outside

alias, function, powershell

Solution

You can't do that because the alias won’t be set until the function is called, and when the function is called the alias is scoped to the function scope, so when the function finishes running - the alias is gone.

If you want the alias to survive, specify use scope parameter with a value of 'global'

Problem

I have a powershell function inside a file myfunc.ps1 ``` function Set-Util ($utilPath ) { if(Test-Path($utilPath) ){ $fullPath = Join-Path -Path $utilPath "util.exe" set-alias MyUtil $fullPath #echo "Path set to $fullPath" }else { throw (" Error: File not found! File path '$fullPath' does not exist.") } } ``` and I dot call it from command line with . .\myfunc.ps1 then call Set-Util somedirectory The alias is being set correctly in the function, but I can't access it out here with MyUtil Should I export the alias because the scope is only in the method? I tried doing so with Export-ModuleMember but got an error saying the cmdlet can only be called from insdie a module.

Original source