Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / PowerShell Profile Files: Getting Started

May 10, 2023 PowerShell

PowerShell Profile Files: Getting Started

PowerShell profile is essentially a regular PowerShell script (PS1) that runs when PowerShell starts and is used as a logon script to configure an environment. You can add your own functions, commands, and aliases, import PowerShell modules, set environment variables, change the appearance and settings of a PoSh console using a PowerShell profile. All profile elements will be automatically applied to each new PowerShell session.

The equivalent of PowerShell profiles in Linux are the profile, .bashrc, .bash_profile files used to automatically run scripts when the shell starts.

There are multiple paths in Windows PowerShell to store profile files. The table below shows all paths by their priority (the highest priority profile comes first):

PathDescription
$PSHOME\Profile.ps1AllUsersAllHosts
$PSHOME\Microsoft.PowerShell_profile.ps1AllUsersCurrentHost
$Home\Documents\PowerShell\Profile.ps1CurrentUserAllHosts
$Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1CurrentUserCurrentHost

If you want to configure a PowerShell session settings for all users of a computer, you must use the $PROFILE.AllUsersAllHosts file. To configure the PowerShell profile for the current user only, save your configuration to $PROFILE.CurrentUserCurrentHost.

To find your PowerShell profile, just run the $Profile command. By default, the path to a user PowerShell profile is C:\Users\username\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

  • In Windows PowerShell, the $Home environment variable refers to the current user profile folder (C:\Users\username). $PsHome refers to the directory PowerShell is installed to (C:\Windows\System32\WindowsPowerShell\v1.0).
  • In new PowerShell Core 7.x versions, the $PSHome refers to C:\Program Files\PowerShell\7.
  • In PowerShell Core for Linux, the profile is located in /opt/microsoft/powershell/profile.ps1 or /usr/local/microsoft/powershell/7/profile.ps1.

PowerShell ISE has its own profile files:

$PsHome\Microsoft.PowerShellISE_profile.ps1All users
$Home\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1Current user

Visual Studio Code also has its own profiles if used as an editor of PowerShell scripts):

$PSHOME\Microsoft.VSCode_profile.ps1All users
$Home\Documents\PowerShell\Microsoft.VSCode_profile.ps1Current user

All paths to profiles can be found in the $PROFILE environment variables

$PROFILE | Get-Member -Type NoteProperty

To access a profile file (for example, the current user profile):

$PROFILE.CurrentUserCurrentHost or $PROFILE

To check if the PowerShell profile of the current user has been created, run the command below:

Test-Path -Path $PROFILE

In this example, a PowerShell profile file for the current user has not been created (False).

By default, PowerShell profile files in Windows are not created.

cheking powershell profile paths in windows

To create a PowerShell profile file, if it does not exist, use the script below:

if (!(Test-Path -Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force
}

You can edit it with any text editor. For example, you can edit a profile file using:

  • Notepad: notepad $profile
  • PowerShell ISE: ise $profile
  • Visual Studio Code: code $profile

Let’s try to configure custom PowerShell profile. In this example we will adjust the color, the console title (it will display the PowerShell version), change the default PowerShell directory, and display the computer name at the PS console prompt:

function CustomizePSConsole {
$Host.ui.rawui.foregroundcolor = "cyan"
$hostversion="$($Host.Version.Major)`.$($Host.Version.Minor)"
$Host.UI.RawUI.WindowTitle = "This is PowerShell $hostversion"
Set-Location 'C:\PS\'
Clear-Host
}
CustomizePSConsole

Let’s show a computer name in the PS console prompt:

function Prompt
{
$env:COMPUTERNAME + " | " + (Get-Location) + "> "
}

For example, you can map a network drive:

New-PSDrive –Name “Tools” –PSProvider "FileSystem" –Root "\\MUN-DEV01\Scripts"

How to Create a PowerShell Profile in Windows

If you often use PowerShell to manage Azure tenants or Microsoft 365, you can display a prompt to connect to your tenant each time you start the console. If you type Y, the following script will prompt you to enter your credentials and connect to your Exchange Online tenant:

$connectM365= Read-Host "Connect to Exchange Online? (Y/N)"
If ($connectM365 -eq "Y"){
$LiveCred = Get-Credential
Connect-ExchangeOnline –Credential $LiveCred
}

You can add the Clear-Host cmdlet to the end of your profile file to clear the console. Save the profile file as Microsoft.PowerShell_profile.ps1 and restart PowerShell. At the next start, all profile settings will be automatically applied to your console and its appearance will change.

customized windows powershell console

Since the PowerShell profile file is a PS1 script file, pay attention to the settings of your PowerShell Execution Policy. By default, PS1 script files (including profiles) are not allowed to run by the Restricted policy. To allow the PowerShell profile to be applied, you must change the PowerShell Execution Policy to Remotesigned. You can configure the settings using a GPO or the following command:

Set-ExecutionPolicy Remotesigned

If you want your PowerShell profile to be applied to remote sessions as well, use the command:

Invoke-Command -Session $s -FilePath $PROFILE

If you want to ignore profile settings when starting a PowerShell session, use –NoProfile option when running PowerShell.exe or pwsh.exe.

1 comment
0
Facebook Twitter Google + Pinterest
previous post
Check Wi-Fi Signal Strength on Windows with PowerShell
next post
Managing Calendar Permissions on Exchange Server and Microsoft 365

Related Reading

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

October 15, 2023

How to Query and Change Teams User Presence...

October 8, 2023

Installing Language Pack in Windows 10/11 with PowerShell

September 15, 2023

1 comment

iamauser May 27, 2022 - 6:59 am

https://ohmyposh.dev/ FTW 😉

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023
  • How to Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Fix: Remote Desktop Licensing Mode is not Configured
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Configuring Port Forwarding in Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • How to Delete Old User Profiles in Windows
  • Get-ADUser: Find Active Directory User Info with PowerShell
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top