http://blogs.technet.com/b/heyscriptingguy/archive/2014/05/21/what-39-s-in-your-powershell-profile-users-39-favorites-part-2.aspx
Today, I want to share what's in my profile. I've customized it a bit more since then.
Here' what it has :
- Change Error color to Gray from default red. It stresses me to see RED RED on the prompt.
- Changing the location to my scripts folder to : Easily call my common scripts, Prevent accidental changes to system32 folder.
- Checking if I've launched Power Shell as an Admin or not.
- Customizing in-built PROMPT function to show ADMIN/DBG mode and increment command number after each command run.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# change Error Color | |
$host.PrivateData.ErrorForegroundColor = "gray" | |
Set-Location "D:\PowerShell" | |
# Customise in-built function "prompt" | |
function prompt | |
{ | |
# The at sign creates an array in case only one history item exists. | |
$history = @(get-history) | |
if($history.Count -gt 0) | |
{ | |
$lastItem = $history[$history.Count - 1] | |
$lastId = $lastItem.Id | |
} | |
$nextCommand = $lastId + 1 | |
$currentDirectory = (Get-Location) | |
# check ADMIN | |
$identity = [Security.Principal.WindowsIdentity]::GetCurrent() | |
$principal = [Security.Principal.WindowsPrincipal] $identity | |
$(if (test-path variable:/PSDebugContext) { '[DBG]: ' } | |
elseif($principal.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) | |
{ "[ADMIN] " }) + "PS: $nextCommand $currentDirectory >" | |
} | |
Write-Host "Welcome $env:UserName, Let's Automate" -Fore Yellow |
Want to know what Power Shell MVP's have ? See here.
If you don't know about profile, in simple terms I'll try to explain.
It's an auto-loading PS1 file which runs each time Power Shell starts. Whatever is written in it is executed and then you can see prompt. the environment variable $Profile tells you the path of the file.
To bypass loading profile, powershell.exe has a parameter called -NoProfile. It's a best practice to use this. If an attacker has control of your system, he can change PROFILE and run arbitrary code. Also, your code may behave differently if someone has changed some functions in it.