Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View Tinus IT Wiki

View my Photo Website

Powershell Profile

published: July 7, 2019 author: Tinu tags: PowerShell categories: PowerShell-Basic


Table of Contents

Create your own PowerShell Profile

Scope

You can create a profile for the following scopes:

Description Path
Current User, Current Host $PROFILE
Current User, Current Host $PROFILE.CurrentUserCurrentHost
Current User, All Hosts $PROFILE.CurrentUserAllHosts
All Users, Current Host $PROFILE.AllUsersCurrentHost
All Users, All Hosts $PROFILE.AllUsersAllHosts

Profile

To create your own Profile for WindowsPowerShell start Visual Studio Code and edit each of the following profile.ps1:

  • Microsoft.PowerShell_profile.ps1
  • Microsoft.VSCode_profile.ps1
  • profile.ps1
code $PROFILE

code $PROFILE.CurrentUserCurrentHost

code $PROFILE.CurrentUserAllHosts

Functions in your Profile

My prefered functions in my own profile.ps1 on Windows, Mac and Linux:

function Test-IsElevated {
    if($PSVersionTable.PSVersion.Major -lt 6){
        # Windows only
        $user = [Security.Principal.WindowsIdentity]::GetCurrent()
        $ret  = (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
    }else{
        if($IsWindows){
            $user = [Security.Principal.WindowsIdentity]::GetCurrent()
            $ret  = (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
        }
        if($IsLinux -or $IsMacOS){
            $ret  = (id -u) -eq 0
        }
    }
    $ret
}

function prompt
{
    if (Test-IsElevated) {
        $color = 'Red'
    }
    else{
        $color = 'Green'
    }
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Import-Module -Name CompletionPredictor
    Set-PSReadLineOption -PredictionViewStyle ListView
    $history = Get-History -ErrorAction Ignore

    $Version = "$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor).$($PSVersionTable.PSVersion.Patch)"

    Write-Host "[$($history.count[-1])] " -NoNewline
    Write-Host "[$([Environment]::UserName)@$([Environment]::MachineName)] " -ForegroundColor $color -NoNewline
    Write-Host ("I ") -nonewline
    Write-Host (([char]9829) ) -ForegroundColor $color -nonewline
    Write-Host ("  PS $Version ") -nonewline

    Write-Host ("$(get-location)") -foregroundcolor $color -nonewline
    Write-Host (" >") -nonewline
    return " "

}

Output, if you start WindowsPowerShell:

[0] [user@computer] I  PS 5.1.1 C:\Users\Admin >

Output, if you start PowerShell:

[0] [user@computer] I  PS 7.1.4 C:\Users\Admin >

See also

About Profiles on Microsoft Docs.


← Previous Post [ Top ] Copyright © 2024 by tinuwalther [ Blog ] Next Post →