Create A PowerShell Profile File | Configure Profile In 5 Easy Steps

The PowerShell profile is a script that starts to run when you launch PowerShell. You can use it to customize the appearance of your console, automatically load PowerShell scripts or modules, and create aliases for cmdlets that you frequently use.

Create A PowerShell Profile File | Configure Profile In 5 Easy Steps

This post will examine the various PowerShell Profile locations, describe how to create a profile, and provide you with some practical examples.

1. Create A Windows PowerShell Profile File

Every time you launch a PowerShell session, a standard script called a PowerShell profile executes. The profile for “current user, current host” may be found at the following locations, depending on the operating system:

Operating SystemPowerShell Profile Location
macOS and Linux~/.config/PowerShell/Microsoft.Powershell_profile.ps1
Windows$Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
  • Run the Test-Path command listed below in PowerShell while logged in as an administrator to see if a PowerShell profile is already present.
Test-Path $profile
Test-Path $profile
  • The command returned False, indicating that no PowerShell profile is yet available.
Test-Path $profile: False
  • There are many profiles for both the current user and all users. You are referring to the ‘Current user – Current Host’ profile, however, when you discuss the Windows PowerShell profile.
  • There are many profiles for both the current user and all users. You are referring to the ‘Current user – Current Host’ profile, however, when you discuss the Windows profile.
ProfilePath
Current User – Current Host$Home\[My ]Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
All Users – Current Host$PSHOME\Microsoft.PowerShell_profile.ps1
Current User – All Hosts$Home\[My ]Documents\WindowsPowerShell\Profile.ps1
All Users – All Hosts$PSHOME\Profile.ps1
New-Item -path $profile -type file –force
New-Item -path $profile -type file –force

Run each of the instructions listed below to verify the location of each user’s PowerShell profile.

  • Return the Current User – Current Host $profile’s PowerShell profile path.
$profile
  • Get the Current User – All Hosts $profile’s profile
$profile.CurrentUserAllHosts
  • Return the All Users – Current Host $profile profile
$profile.AllUsersCurrentHost
  • Return the All Users – Current Host $profile profile
$profile.AllUsersAllHosts
$profile.AllUsersAllHosts

2. Create A Powershell Profile: Custom Method

PowerShell profiles are first established with a blank default. You can save time by using your PowerShell profile to save variables, aliases, and commands that you often use.

PowerShell disables script execution by default to stop malicious scripts from harming the system, thus you may want to change this setting.

To change the execution policy (Set-ExecutionPolicy) of PowerShell to RemoteSigned, run the command below.

If the script was not produced on the system where it is being run, it must be signed according to the RemoteSigned policy. The scripts must have a digital signature in order to comply with this policy, whether they are downloaded over the Internet or by programs like Internet Explorer, Outlook, or Messenger.

Set-ExecutionPolicy RemoteSigned
Set-ExecutionPolicy RemoteSigned

Run one of the following commands to open your PowerShell profile ($PROFILE) in PowerShell ISE or Notepad but without producing any output.

# Opens Microsoft PowerShell profile in PowerShell ISE

ise $PROFILE 
ise $PROFILE 

# Opens PowerShell profile in Notepad

notepad $PROFILE
notepad $PROFILE

3. Aliasing Quick Powershell Commands and Functions 

The most popular customizations are accessibility aliases, which let you call PowerShell commands by any name of your choosing.

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


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

4. Changing the Look and Feel of the PowerShell Console

With a customized prompt, you can modify PowerShell’s functionality and improve its interactive experience.

To reflect the modifications to the default settings, add the following information to your PowerShell profile. After saving the changes, close the file.

  • PowerShell’s default appearance is altered via the Color-Console function below, which also displays the current date and 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
 . $profile

The following modifications to your profile will be reflected on the PowerShell console.

  • To test your network connection, use the tn alias you created in your PowerShell profile rather than the Test-NetConnection cmdlet. The notepad can also be opened by using np.

# Test network connection

TN

# Open notepad

np

5. Giving Parameters Default Values

Use the $PSDefaultParameterValues preference variable rather than repeatedly entering a difficult-to-remember parameter value. The ability to define unique values for a cmdlet or function is a useful aspect of this functionality.

The preference variable $PSDefaultParameterValues is a hash table with entries that consist of a key and a value. The pattern for keys is cmdlet: parameters and values can either be script blocks or parameters (which can take the form of strings, booleans, or integers).

The $PSDefaultParameterValues preference variable can be used generally as follows:

$PSDefaultParameterValues=@{"CmdletName:ParameterName"="DefaultValue"}

See the following example to use the $PSDefaultParameterValues preference variable:

  • Run the command below to set the Verbose parameter to True for all commands (*), but it doesn’t print anything. The $PSDefaultParameterValues hash table will gain a new item.
$PSDefaultParameterValues=@{"*:Verbose"=$True}
  • To check the default parameter variable value, execute the $PSDefaultParameterValues function without any parameters.
$PSDefaultParameterValues
  • Run the tn alias shown below to check your network connectivity now.
TN
  • Since the Verbose parameter value is set to True by default for all commands, you may view the verbose details as the command executes.
Meet the Author

Abdul Rahim has been working in Information Technology for over two decades. Learn how Abdul got his start as a Tech Blogger , and why he decided to start this Software blog. If you want to send Abdul a quick message, then visit his contact page here.