Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • 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 2012
    • 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 25, 2022 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):

Path Description
$PSHOME\Profile.ps1 AllUsersAllHosts
$PSHOME\Microsoft.PowerShell_profile.ps1 AllUsersCurrentHost
$Home\Documents\PowerShell\Profile.ps1 CurrentUserAllHosts
$Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 CurrentUserCurrentHost

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.ps1 All users
$Home\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1 Current user

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

$PSHOME\Microsoft.VSCode_profile.ps1 All users
$Home\Documents\PowerShell\Microsoft.VSCode_profile.ps1 Current 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

Using Previous Command History in PowerShell Console

January 31, 2023

How to Install the PowerShell Active Directory Module...

January 31, 2023

Finding Duplicate E-mail (SMTP) Addresses in Exchange

January 27, 2023

How to Disable or Uninstall Internet Explorer (IE)...

January 26, 2023

How to Delete Old User Profiles in Windows?

January 25, 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

  • Using Previous Command History in PowerShell Console

    January 31, 2023
  • How to Install the PowerShell Active Directory Module and Manage AD?

    January 31, 2023
  • Finding Duplicate E-mail (SMTP) Addresses in Exchange

    January 27, 2023
  • How to Delete Old User Profiles in Windows?

    January 25, 2023
  • How to Install Free VMware Hypervisor (ESXi)?

    January 24, 2023
  • How to Enable TLS 1.2 on Windows?

    January 18, 2023
  • Allow or Prevent Non-Admin Users from Reboot/Shutdown Windows

    January 17, 2023
  • Fix: Can’t Extend Volume in Windows

    January 12, 2023
  • Wi-Fi (Internet) Disconnects After Sleep or Hibernation on Windows 10/11

    January 11, 2023
  • Adding Trusted Root Certificates on Linux

    January 9, 2023

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Configuring Port Forwarding in Windows
  • Installing RSAT Administration Tools on Windows 10 and 11
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11?
  • Adding Drivers into VMWare ESXi Installation Image
Footer Logo

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


Back To Top