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

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:

code $PROFILE

code $PROFILE.CurrentUserCurrentHost

code $PROFILE.CurrentUserAllHosts

Functions in your Profile

My prefered functions in my own profile.ps1

Windows

function Test-IsAdministrator {
    $user = [Security.Principal.WindowsIdentity]::GetCurrent();
    (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

function prompt{

    if (Test-IsAdministrator) {
        $color = 'Red'
    }
    else{
        $color = 'Green'
    }
    
    $history = Get-History -ErrorAction Ignore
    $vscode  = (code --version)[0]
    $Version = "$($PSVersionTable.PSVersion.ToString())"
    Write-Host "[$($history.count[-1])] " -NoNewline
    Write-Host ("$(($env:UserName).ToLower())@$(($env:ComputerName).ToLower()) [VS Code $($vscode)]") -nonewline -foregroundcolor $color
    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 -foregroundcolor $color
    return " "

}

Mac OS

function Test-IsRoot {
    return ((id -u) -eq 0)
}

function prompt{

    if (Test-IsRoot) {
        $color = 'Red'
    }
    else{
        $color = 'Green'
    }

    $history = Get-History -ErrorAction Ignore
    $vscode  = (code --version)[0]
    $Version = "$($PSVersionTable.PSVersion.ToString())"
    Write-Host "[$($history.count[-1])] " -NoNewline
    Write-Host ("$((id -un).ToLower())@$((hostname).ToLower()) [VS Code $($vscode)]") -nonewline -foregroundcolor $color
    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.

[ Top ] [ Blog ]