PowerShell is a powerful command line shell that is great for Windows systems administration. It provides a very simple way to query local and remote systems, set variables and run scripts. As of right now there are two flavors of PowerShell. There is the PowerShell that comes packaged with Windows OS, which is PowerShell 5.1, and then there is PowerShell Core. PowerShell Core is the open-source version of PowerShell that is usable on Windows as wells as other systems such as Linux-based systems. As of this post, PowerShell Core is on version 7.2.5. You can visit the GitHub repo here.

I tend to just stick with the standard version because that is what is available at work. One question that I get asked is, “Why don’t you just use good ol’ CMD?” The reason I use PowerShell over CMD is because PowerShell can get more granular and has simple-to-use filtering commands. Plus, you can run all CMD commands within PowerShell. Today, I want to talk about PowerShell profiles.

PowerShell Profiles

PowerShell comes with tons of useful commands but what if you want to create your own function? Well, you can! If you open a PowerShell prompt, you can create a function and use in that environment. As soon you close that shell, the function is deleted. Next time you open PowerShell, the function will not be there. This is where PowerShell profiles comes in. Once created, the profile file lives inside of your Windows Profile that is loaded every time PowerShell is loaded. Within this file you can create variables, functions, set shell properties and so much more. When PowerShell loads, everything in this file will be usable.

Note: You will have to change your Execution Policy to have PowerShell load your profile.

Creating Your PowerShell Profile

To create your own PowerShell profile, open PowerShell and run the following command:

PS> New-Item -Path $profile -Type File -Force

The output will show the directory along with the profile file. From here you can open your profile by running notepad $profile in the shell. This will open your profile in Notepad.

Adding Stuff to Your PowerShell Profile

Since we have the profile open, let’s create a variable and a simple function. Just like in the shell you would treat each line as a line of executable code except for stuff that is inside of parenthesis and curly brackets, such as code blocks. Let’s say we wanted to create a variable that holds the current date. We would write something along the lines of:

$today = Get-Date

If you save and then reopen PowerShell, you can just type $today and hit the return key and voilà, there is the date. Now let’s try a multiline function. Let’s create a function that adds two numbers together and then prints the sum called Get-Sum:

Function Get-Sum {
  Param (
	[int]$firstNumber,
	[int]$secondNumber
  )

  $sumOfNumbers = $firstNumber + $secondNumber
  Write-Output $sumOfNumbers
}

Once again, we save and restart PowerShell to make our new function usable. Now we can run something like this that spits out the result:

PS> Get-Sum 2 3
5

Pretty cool! Hopefully this can help you get started with your PowerShell profile.