Set PowerShell Aliases with a script

5

3

I created a PowerShell script to set aliases for a PowerShell session. When I run the script and try the alias, I get the error saying that the alias isn't recongized. How can I use the aliases that are set by the script?

thecomputerguru

Posted 2019-12-10T21:19:56.680

Reputation: 329

Answers

6

Run the script using Dot sourcing operator:

. Dot sourcing operator
Runs a script in the current scope so that any functions, aliases, and variables that the script creates are added to the current scope.

. c:\scripts\sample.ps1

Note: The dot sourcing operator is followed by a space. Use the space to distinguish the dot from the dot (.) symbol that represents the current directory.

In the following example, the Sample.ps1 script in the current directory is run in the current scope.

. .\sample.ps1

JosefZ

Posted 2019-12-10T21:19:56.680

Reputation: 9 121

Is that using the absolute path, not a relative path? – thecomputerguru – 2019-12-10T23:08:56.283

The former example . c:\scripts\sample.ps1 shows an absolute path while the latter example . .\sample.ps1 shows a relative one. – JosefZ – 2019-12-11T15:02:53.353