Powershell

PowerShell Profile | A Detailed Guide

PowerShell has a wide range of support for a variety of operating systems, including macOS, Windows, Linux, etc. It is a scripting-based language that gives a facility to automate and configure various tasks. A PowerShell profile is basically a script that executes in the console environment when a user starts a new session.

A PowerShell profile may comprise a set of cmdlets, functions, and aliases to automate a set of requirements for the user. An overview of the PowerShell profile is provided in this guide. Moreover, you will also learn to create as well as customize the PowerShell profile.

How to Create a PowerShell Profile?

The PowerShell profile is not available for use by default. You need to create it before customizing various operations using PowerShell. A PowerShell profile can be created with the help of the New-Item cmdlet or using the notepad command. This section lists both methods for creating a PowerShell profile.

Note: It is preferred to use the most recent PowerShell 7. This guide uses PowerShell 7 for creating and using the PowerShell profile. You can switch to the latest PowerShell version using the following link:

Update your PowerShell version.

Method 1: Create a PowerShell Profile Using the New-Item Cmdlet

Generally, the New-item cmdlet is used to create a new file or directory in PowerShell. However, it also assists in creating a PowerShell profile by following the below-mentioned steps:

Step 1: Open PowerShell 7

Search for PowerShell 7 in the search bar and click on “Run as Administrator” to open PowerShell 7 with root privileges:

Step 2: Check the existence of the profile

In the second step, check whether the profile is already created or not via the following Test-Path cmdlet:

Test-Path $Profile

The returned output is false, which states the PowerShell profile has not yet been created.

Step 3: Create the Profile

Create a file named “profile.ps1” using the New-Item cmdlet as we did here:

New-Item -Path $Profile -Type File -Force

The successful execution of the command verifies the creation of the PowerShell profile. However, you can verify it via the below-stated command:

Test-Path $Profile

The output returns true, showing that the creation of the profile is done in PowerShell.

Method 2: Create a PowerShell Profile Using Notepad

In this method, step-by-step instructions are discussed for building a PowerShell profile using Notepad.

Step 1: Create a profile

The notepad can be launched from PowerShell using the notepad keyword. The following command will create an empty PowerShell profile:

notepad $PROFILE

Step 2: Check the existence of the profile

In the second step, check whether the profile is already created or not via the following Test-Path cmdlet:

Test-Path $Profile

The display returns “True“, which shows the verification of the profile existence in PowerShell.

How to Customize a PowerShell Profile

The PowerShell profile needs to be customized to be used by other users. If the user scope is not defined, then the profile will only be accessible to the current user. You can customize a PowerShell profile for other users by using one of the following syntaxes:

Profile Type Profile Path/ Directory
Current User
$PROFILE or $PROFILE.CurrentUserCurrentHost
All users
$PROFILE.AllUsersCurrentHost
All users and all hosts
$PROFILE.AllUsersAllHosts
Current user and all hosts
$PROFILE.CurrentUserAllHosts

For example, you can allow all the users to access the profile. For this purpose, the first command is executed. While the second command verifies that the profile is accessible to all users or not:

$PROFILE.AllUsersAllHosts
Test-Path $Profile.AllUsersAllHosts

The output ensures that the output returned is true, which states that the profile has been customized for all the users and the hosts.

Conclusion

A PowerShell profile comprises a set of aliases, cmdlets, functions, etc., to perform a specific sequence of tasks for a specific user. PowerShell’s profile scope can be set for the current as well as for all users. A PowerShell profile can be created using the New-Item cmdlet. By default, it is used for the current user. However, the profile can be created for multiple/all users as well. This post has provided details for the PowerShell profile.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.