Setting an Alias in Windows Powershell.

It’s no secret that the UNIX terminal is a far superior and preferred method for command-line computing. Windows, it seems, is well-aware of this and determined to improve the command line tools on its Operating Systems.

VScode has already come such a long way. In my opinion, their open-source editor is the best on the market. It seems there’s been a change in direction at Windows as of late, as they ease towards a more open-source and user-centric approach. This is exemplified in Microsoft’s new Windows Terminal, a UNIX-style terminal under development for Windows. There’s already Linux subsystem tools available on some distributions, but it’s finicky at best in its current state.

While I’m hopeful for the future, I’ve come to enjoy how much closer Windows Powershell is to a UNIX system in terms of syntax and functionality. It isn’t perfect, but I’m determined to make it as close and comfortable as possible.

Setting an Alias in Powershell

There’s a lot of conflicting information about there, and the Microsoft docs are kind of vague. After doing some research, here’s what I’ve gathered:

  • It’s not a good idea to add this directly to your $env:WINDIR (sort of like a UNIX root)
  • The recommended way is to add it via a user profile

Steps from here vary, but I’ve tested this method across a few systems and it seems to be effective.

  • You’ll want a profile1.psl file. This isn’t created by default. To create the profile1.psl file, open up Powershell and type in the following command:
  • new-item $PROFILE.CurrentUserAllHosts -ItemType file -Force
  • to access the file, type in the next command:
  • ise $PROFILE.CurrentUserAllHosts
  • (Conditional) This should be enough, but if you have an issue with this that means you need to change your execution policy: run
    Set-ExecutionPolicy -Scope CurrentUser then RemoteSigned then run the ise command again.

Otherwise, the first two commands should be enough to create the profile then pull it up in Windows Powershell ISE (Integrated Scripting Environment) – it looks like this:

From here, we can set alias’ that call functions. You can see in my example that I’ve written a function that:

  • Changes directory
  • Opens Google in a web browser
  • Removes a file in the current directory named fileName.txt

All we have to do now is set up an alias that calls that function. Anywhere below the closing bracket of the function we can write:

Set-Alias google goSomewhereThenOpenGoogleThenDeleteSomething

Now save, exit the ISE and Powershell and restart. You should be able to type google as a command with nothing else to run the function you just created. It might throw an error saying: rm : Cannot find path 'C:\Users\fileName.txt' because it does not exist. which is expected but if it opens Google in a browser, that means it was successful.

Handy to do

I’ve set Alias’ for a lot of things but most handy are to set alias’ to check your profile’s location and open it in case you want to add more, you won’t have to remember the steps.

I use profile which displays:

and as you can see above, profile_open which runs the command to open my profile in the ISE, ise $PROFILE.CurrentUserAllHosts

In addition to that, I set commonly used scripts and programs on Alias for convenience so I don’t have to navigate to a directory and execute with the program prefix. E.g. my most recent ratelimit script I wrote for work, I just type ratelimit from anywhere to start the program:

Rudy

I believe in conscious continuous improvement.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.