Wednesday, January 22, 2020

Setting Up Custom PowerShell Scripts On Terminal Load



So I am used to having my own shell functions load up automatically when I open my terminal window to perform various operations on a daily basis. In other words I prefer having my own short cuts as functions or aliases for commands that I need and use on a daily basis. For those who want to know more about the reason for this you may refer to this older article

One day a landed in an organization where the team preferred using Windows OS over others. If you have read the article above you will understand that this was a different ball game for me, and obviously I wanted to have the same effect on Windows OS. Yes with the latest Windows OS 10 one can set up bash, but then how would I ever learn something new, in this case PowerShell.

Oh well ... 

Let's Get Started

We will be looking at a step by step process of setting up  your own custom PowerShell functions which will load automatically every time you open your PowerShell terminal. This will simplify working with those long complex commands which you probably need to use every day.


1. Locate Or Create The Default Folder

  • Open up Windows Explorer and head over to the "Documents" folder.
  • Look for a folder with the name "WindowsPowerShell". If the folder is not there then create it. This folder is the home / default directory for your PowerShell profile. Make sure you use that specific name.
  • Open the folder. Just note that if you did not have to create this folder and it was already there then it will probably contain a have a sub folder named "Scripts", which you can ignore right now.

2. Creating Your Custom PowerShell Script / Function

  • While you're still in that "WindowsPowerShell" folder, create a new PowerShell script file i.e I created mine with the name and extension Message-Printer.ps1 and please note that you can name it whatever you want, just keep the extension ".ps1" and that's it.
  • Open up the file in your favorite text editor or IDE, I prefer VS Code for most of my scripting. I also think that in this case it works out because it's a Microsoft IDE which means there's good support for PowerShell out of the box. So go ahead and open that file to start editing the as follows: 

function showThisMessage() {
    # Print out the message
    Write-Host "Welcome to PowerShell automation!"

  • Ultimately the following image shows how things should be on you machine at this point. Have a look to confirm and then we can carry on.

3. Configuring Auto Load Of Your Script  

For each time you open your powershell terminal, there's a session that gets created and that session has some sort of profile that it's running with it. When a user does not have any custom profiles setup then they can work with the default one. For example we are going to work with a default one which will be activated when we open up the PowerShell terminal. 

NB: You may click the following link for more info about PowerShell Profiles. Let's get to the work...

  • Create a new file inside your "WindowsPowerShell" folder with the following name, Microsoft.PowerShell_profile.ps1, keep the name of file exactly like that since that's the default profile naming.
  • Open up the file in your favourite text editor or IDE.

 # This code may show as two lines but, please write it in one line.
 Get-ChildItem -Path $PSScriptRoot\Message-Printer.ps1 | Foreach-Object { . $_.FullName } 

  • For more validaton you may refer to the image below: 

  • So what's happening in that line is basically a search for our new custom script, "Message-Printer.ps1". This search will then import this script into your PowerSehll session by default when a new PowerShell terminal tab is opened. This is the PowerShell sytax and way of loading other scripts. It's achieved through the power of the Get-ChildItem commandlet which is used to get the specified file locations on the file system. 
  • The actual path is picked up dynamically with the $PSScriptRoot part of the script and then later we can suffix with our file and its extension.
  • And finally the Foreach-Object commandlet used to perform the same operation on a collection of files.

4. Running The Custom Script

Great! So now make sure you have saved all your files from your editor or IDE and then give your script a test.
  • Open up the PowerShell terminal.
  • Immediately start typing the function you created earlier. So just type showThisMessage in your terminal tab followed by the "return" or "enter" key ... and BOOM! Your text show be displaying as follows: 




So there you go! You now know to automatically load up your custom PowerShell scripts. Hoping it was helpful and many of you will try and use this method.