• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / PowerShell Profiles

PowerShell Profiles

11/09/2018 by Stephanos Leave a Comment

PowerShell Profiles

In this tutorial we will see about PowerShell Profiles and their use. PowerShell profiles help you to customize your environment and add elements for every PowerShell session that you start. PowerShell profile is a script that runs every time we will start PowerShell. You are able to use a PowerShell profile as a logon script to customize the environment. By using a PowerShell profile, you can add commands, aliases, functions, variables, snap-ins, modules, PSDrives and any other sessions-specific elements in you evnironmet. You will not have to import or re-create them every time you run PowerShell. It will be loaded automatically. PowerShell supports several profiles for users and host programs. PowerShell does not create any profiles for you.

If you want session-specific commands, variables, preference variables, aliases, functions, commands (except for Set-ExecutionPolicy), and PowerShell modules that you use frequently, you can use profiles to load in all future session.

[adinserter name=”In Article”]

Profile Files

PowerShell console supports the following profiles:

  • $Home\[My]Documents\PowerShell\Microsoft.PowerShell_profile.ps1 – Current user, Current Host
  • $Home\[My]Documents\PowerShell\Profile.ps1 – Current User, All Hosts
  • $PsHome\Microsoft.PowerShell_profile.ps1 – All Users, Current Host
  • $PsHome\Profile.ps1  – All Users, All Hosts

The profiles above are shown in precedence order. The first profile in the list has the highest precedence order. As you can see in the profiles above, we have two variables. The $PSHome and $Home variables present the following values.

  • $PSHome –  Stores the installation directory for PowerShell
  • $Home  – Stores the current user’s home directory

Other programs that host PowerShell can support their own profiles. PowerShell Integrated Scripting Environment (ISE) supports the following host-specific profiles.

  • $Home\[My]Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1 – Current user, Current Host
  • $PsHome\Microsoft.PowerShellISE_profile.ps1  – All users, Current Host

As per Microsoft, in PowerShell Help, the “CurrentUser, Current Host” profile is the profile most often referred to as “your PowerShell profile”.

[adinserter name=”In Article”]

Profile Variable

In PowerShell we have an automatic variable called $Profile. All PowerShell profile paths that are available in the current session are stored in $Profile variable. By viewing the $Profile variable, you will see the path of the profile.

PowerShell Profiles 1

The $Profile variable stores the path to the “Current User, Current Host” profile. The other profiles are saved in note properties of the $Profile variable.

PowerShell Profiles 2

You can specify a profile by using the properties of the variable. In Windows PowerShell Console, we have the following values in the $Profile variable.

  • $Profile – Current User, Current Host
  • $Profile.CurrentUserCurrentHost – Current User, Current Host
  • $Profile.CurrentUserAllHosts – Current User, All Hosts
  • $Profile.AllUsersCurrentHost – All Users, Current Host
  • $Profile.AllUsersAllHosts  – All Users, All Hosts

You are able to use $Profile variable in your commands.

Code:

Test-Path -Path $PROFILE
Test-Path -Path $PROFILE.CurrentUserCurrentHost
Test-Path -Path $PROFILE.CurrentUserAllHosts
Test-Path -Path $PROFILE.AllUsersCurrentHost
Test-Path -Path $PROFILE.AllUsersAllHosts

Output:

PowerShell Profiles 3

The above commands test if the specified profile has been created. As you can see the answer is false to all of them. As you we have mentioned before, profiles are not created by default. You will have to create them.

[adinserter name=”In Article”]

Create a profile

In order to create a profile you need to use the following command.

Code:

New-Item -ItemType File -Path $profile -Force

The above command will create a profile for “Current User, Current Host”.

Note that the above will replace any existing profile. If you want to ensure that you will not replace any profile that already exists and lose the changes that you have applied already, you can try the below.

Code:

if (-not (Test-Path -Path $profile.AllUsersAllHosts)){
    New-Item -ItemType File -Path $profile.AllUsersAllHosts -Force}

The above command will check if there is a profile for “All Users, All Hosts” and if it does not exists it will create the profile.

In order to create a profile you will need to run PowerShell console as administrator. This applies from Windows Vista and later versions.

[adinserter name=”In Article”]

Edit a profile

If a profile exists and you need to apply some changes or additions, you need to use a text editor to edit the profile. By using notepad is an easy way to edit the profile.

Code:

notepad $profile

As you can see below my current profile is configured to provide a Welcome message when I open my console.

PowerShell Profiles 4

Now I will use the above command to apply some changes to my profile.

PowerShell Profiles 5

Below are the changes that I have applied.

PowerShell Profiles 6

Then when I have restarted PowerShell console, as you can see the changes have been applied.

PowerShell Profiles 7

[adinserter name=”In Article”]

Profile Guidelines

Below I will provide some guidelines on for setting up your profiles, as provided by Microsoft.

If you use multiple host applications, put the items that you use in all the host applications into your $Profile.CurrentUserAllHostsprofile. Put items that are specific to a host application, such as a command that sets the background color for a host application, in a profile that is specific to that host application.

If you are an administrator who is customizing PowerShell for many users, follow these guidelines:

  • Store the common items in the $profile.AllUsersAllHosts profile
  • Store items that are specific to a host application in $profile.AllUsersCurrentHost profiles that are specific to the host application
  • Store items for particular users in the user-specific profiles

Be sure to check the host application documentation for any special implementation of PowerShell profiles.

No Profile

Even if you have create profiles to load each time you open your console, you are allowed to run PowerShell without any profile to be loaded. You can do this by running PowerShell with -NoProfile parameter. This can be done from cmd.exe, Run dialog box or PowerShell itself.

Code:

PowerShell -NoProfile

Execution Policy

The PowerShell execution policy determines whether you can run scripts and load configuration files, including the profiles. The “Restricted” execution policy is the default. You can find more information about PowerShell execution policy here. If you use the “Restricted” policy, it prevents all scripts from running, including the profiles. Contents of the profile are not applied. A Set-ExecutionPolicy command sets and changes your execution policy in all PowerShell sessions because the value is saved in the registry. You do not have to set it when you open the console, or store a Set-ExecutionPolicy command in your profile.

[adinserter name=”In Article”]

Profile on remote session

PowerShell profiles are not run automatically in remote sessions, so the commands that the profiles add are not present in the remote session. In addition, the $Profile automatic variable is not populated in remote sessions.

If we want to run the profile from our local computer, to a remote session, we need to used Invoke-Command cmdlet.

The example below will run the local profile to a remote session.

Code:

$MySession = New-PSSession -ComputerName RemoteComputer
Invoke-Command -Session $MySession -FilePath $profile

Output:

PowerShell Profiles 8

As profile will not be populated in remote session, we can also load the remote profile into the session. Check the below example.

Code:

$MySession = New-PSSession -ComputerName RemoteComputer
Invoke-Command -Session $MySession -ScriptBlock {. "$env:USERPROFILE\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"}

Output:

PowerShell Profiles 9

I hope the tutorial about PowerShell Profiles is helpful.

Please let me know your comments and thoughts.

You feedback is appreciated.

[adinserter name=”In Article”]

Related Links

  • PowerShell Tutorials
  • PowerShell Scripts
  • about_Profiles | Microsoft Docs
  • PowerShell Automatic Variables
  • PowerShell Logical Operators
  • PowerShell Execution Policy
  • Test-Path – Microsoft Docs
  • New-Item – Microsoft Docs
  • Write-Output – Microsoft Docs
  • Set-ExecutionPolicy – Microsoft Docs
  • Invoke-Command – Microsoft Docs

[adinserter name=”Matched-Content”]

Summary
PowerShell Profiles
Article Name
PowerShell Profiles
Description
PowerShell Profiles. In this tutorial you will find information about PowerShell Profiles and their use. Stephanos Constantinou Blog
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: Invoke-Command, New-Item, PowerShell If, Set-ExecutionPolicy, Test-Path, Write-Output

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact