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.
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.
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:
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.
Now I will use the above command to apply some changes to my profile.
Below are the changes that I have applied.
Then when I have restarted PowerShell console, as you can see the changes have been applied.
[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.CurrentUserAllHosts
profile. 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:
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:
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”]


Leave a Reply