Take Advantage of Your Profile

by Sep 29, 2022

When PowerShell launches, it automatically looks for a special autostart script. It does not exist by default and is different for each PowerShell environment. Its path is revealed in $profile. This is what the path looks for my machine, and while inside a Windows PowerShell console:

 
C:\Users\tobias\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
 

You can easily check whether the file exists, and if not, let PowerShell create it:

$exists = Test-Path -Path $profile
if ($exists -eq $false)
{
    $null = New-Item -Path $profile -ItemType File -Force
}

notepad $profile

Once you have such an autostart script, you can add all kinds of useful things to it that you may need in every PowerShell session of yours. For example, create a shorter prompt:

function prompt
{
    'PS> '
    $host.UI.RawUI.WindowTitle = Get-Location
}

Or make life easier with simplified logons:

function in365 
{
    Import-Module ExchangeOnlineManagement
    Connect-ExchangeOnline -UserPrincipalName 'youremailhere'
}

function out365 
{
    Disconnect-ExchangeOnline -Confirm:$false
}

Just make sure you save your changes, and execution policy allows scripts to run.

 


Twitter This Tip! ReTweet this Tip!