PowerShell Profile : A Getting Started Guide

Published:23 November 2022 - 5 min. read

Uzma Younas Image

Uzma Younas

Read more tutorials by Uzma Younas!

Block over 3 billion compromised passwords & strengthen your Active Directory password policy. Try Specops Password Policy for free!

Have you ever felt tired of running the exact commands or scripts repeatedly? Why not let a PowerShell profile make your life easier?

A PowerShell Profile is a script composed of written instructions that execute when you launch a PowerShell session. And in this tutorial, you will learn many ways to configure your PowerShell profile.

Sounds interesting? Stay tuned and automate custom code executions!

Prerequisites

  • A Windows or Linux computer – This tutorial uses Windows 11 PC.
  • PowerShell 7 or 5.1 installed on your system.

Creating a PowerShell Profile

A PowerShell profile is a regular script that runs each time you start a PowerShell session. Depending upon the operating system, the PowerShell profile for ‘current user, current host’ is located at the following:

Operating SystemPowerShell Profile Location
Windows$Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
macOS and Linux~/.config/powershell/Microsoft.Powershell_profile.ps1

To create a PowerShell profile:

1. Open PowerShell as administrator, and run the below Test-Path command to check if a PowerShell profile already exists.

 Test-Path $profile

Below, you can see the command returned False, which confirms there is no existing PowerShell profile yet.

Checking for an existing PowerShell profile
Checking for an existing PowerShell profile

There are, in fact, different profiles for the current user and all users, as shown below. But when talking about the Windows PowerShell profile, you are referring to the ‘Current user – Current Host’ profile.

Windows PowerShell 5.1

ProfilePath
Current User – Current Host$Home\[My ]Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Current User – All Hosts$Home\[My ]Documents\WindowsPowerShell\Profile.ps1
All Users – Current Host$PSHOME\Microsoft.PowerShell_profile.ps1
All Users – All Hosts$PSHOME\Profile.ps1

💡 The $Home variable references the current users home directory while the $PSHOME variable references the installation path of PowerShell.

PowerShell 7.x

ProfilePath
Current User – Current Host – Windows$Home\[My ]Documents\Powershell\Microsoft.Powershell_profile.ps1
Current User – Current Host – Linux/macOS~/.config/powershell/Microsoft.Powershell_profile.ps1
Current User – All Hosts – Windows$Home\[My ]Documents\Powershell\Profile.ps1
Current User – All Hosts – Linux/macOS~/.config/powershell/profile.ps1
All Users – Current Host – Windows$PSHOME\Microsoft.Powershell_profile.ps1
All Users – Current Host – Linux/macOS/usr/local/microsoft/powershell/7/Microsoft.Powershell_profile.ps1
All Users – All Hosts – Windows$PSHOME\Profile.ps1
All Users – All Hosts – Linux/macOS/usr/local/microsoft/powershell/7/profile.ps1

2. Next, run the following New-Item command to create your PowerShell profile. This command creates a script file in the Current User – Current Host profile path ($profile).

New-Item -path $profile -type file –force
Creating a new PowerShell profile
Creating a new PowerShell profile

Now, run each of the below commands to check the path of the PowerShell profile for each user.

# Return the PowerShell profile path of the Current user - Current host
$profile
# Return the PowerShell profile path of the Current user - All hosts
$profile.CurrentUserAllHosts
# Return the PowerShell profile path of the All users - Current host
$profile.AllUsersCurrentHost
# Return the PowerShell profile path of the All users - Current host
$profile.AllUsersAllHosts

The outputs below display the path of each profile for PowerShell 5 and 7.

Checking PowerShell 5 profiles location
Checking PowerShell 5 profiles location
Checking PowerShell 7 profiles location
Checking PowerShell 7 profiles location

Customizing a PowerShell Profile

A newly-created PowerShell profile is blank by default. You can include variables, aliases, and commands you frequently use in your PowerShell profile without recreating them in each session.

But first, you may want to change PowerShell’s execution policy since PowerShell disables the execution of scripts by default to prevent malicious scripts from affecting the system.

Run the command below to set PowerShell’s execution policy (Set-ExecutionPolicy) to RemoteSigned.

The RemoteSigned policy dictates that any script should be signed if not created on the system where you run them. This policy requires a digital signature for the scripts to come from the internet or be downloaded by applications such as Internet Explorer, Outlook, or Messenger.

Set-ExecutionPolicy RemoteSigned
Setting execution policy to RemoteSigned
Setting execution policy to RemoteSigned

Now, run either of the following commands, which do not provide output, but open your PowerShell profile ($PROFILE) in PowerShell ISE or Notepad.

# Opens PowerShell profile in PowerShell ISE
ise $PROFILE 
# Opens PowerShell profile in Notepad
notepad $PROFILE

Setting Aliases for Quick Commands and Functions Accessibility

Aliases are the most common customizations, as they let you refer to PowerShell commands by your chosen name.

Add the following aliases to your PowerShell profile, save the changes but do not close the file yet.

# Create aliases for frequently used applications.
New-Alias np Notepad.exe
     
# Create aliases for frequently used commands
Set-Alias tn Test-NetConnection

Customizing How the PowerShell Console Looks

Apart from aliases, you can also change how PowerShell operates and enhance its interactive experience with a personalized prompt.

1. Include the following content in your PowerShell profile to reflect the changes in default settings, save the changes and close the file.

The Color-Console function below changes the default appearance of PowerShell by changing its color and displaying the current date/time.

# Create a function to change colors in PowerShell
function Color-Console {
	$Host.ui.rawui.backgroundcolor = "darkcyan"
	$Host.ui.rawui.foregroundcolor = "white"
	$hosttime = (Get-ChildItem -Path  $PSHOME\\PowerShell.exe).CreationTime
	$Host.UI.RawUI.WindowTitle  =  "PowerShell ($hosttime)"
	Clear-Host
}
 
# Calls the Color-Console function
Color-Console

2. Now, run the below command to reload your PowerShell profile.

 . $profile

You will see the following changes to your profile reflected on the PowerShell console.

Reloading the PowerShell profile
Reloading the PowerShell profile

3. Lastly, run the tn alias you set in your PowerShell profile instead of the Test-NetConnection cmdlet to test your network connection. Likewise, you can use np to open the notepad.

# Test network connection
tn
# Open notepad
np
Testing network connection by running an alias of Test-NetConnection cmdlet
Testing network connection by running an alias of Test-NetConnection cmdlet

Supplying Default Values for Parameters

Instead of repeatedly specifying a hard-to-remember particular parameter value, why not use the $PSDefaultParameterValues preference variable? This feature is handy as it lets you specify custom values for a cmdlet or function.

The $PSDefaultParameterValues preference variable is a hash table with entries in two parts: the key and the value. Keys must match the pattern cmdlet:parameter, where values can be either a parameter (a string, boolean, or integer) or a script block.

The general syntax for using the $PSDefaultParameterValues preference variable is as follows:

[email protected]{"CmdletName:ParameterName"="DefaultValue"}

To see the $PSDefaultParameterValues preference variable in action:

1. Run the below command, which does not provide output, but sets the Verbose parameter True for all commands (*). A new entry will be added to the $PSDefaultParameterValues hash table.

[email protected]{"*:Verbose"=$True}

2. Next, run the $PSDefaultParameterValues variable without parameters to verify the default parameter variable value.

$PSDefaultParameterValues
Changing default values for parameters
Changing default values for parameters

Now, run the following tn alias to test your network connection.

tn

Below, you can see the verbose details as the command runs since the Verbose parameter value is set to True by default for all commands.

Testing the $PSDefaultParameterValues preference variable
Testing the $PSDefaultParameterValues preference variable

Conclusion

In this tutorial, you came across several features of the PowerShell profile and changed the looks of your PowerShell console. But more importantly, you learned how PowerShell lets you define and reuse frequently used scripts.

With this newfound knowledge, if you are mainly a PowerShell user, why not add new aliases and scripts to run on startups to assist with your workflow? Having a PowerShell profile configured indeed is a lifesaver!

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!