OpsaC - Operating as PowerShell code
published: July 7, 2019 author: Tinu tags: PowerShell categories: PowerShell-Basic
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 |
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
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 >
About Profiles on Microsoft Docs.