«

»

Sep 26

Print this Post

PowerShell Profile

This is basically the profile posts from http://www.snowland.se (which is written as different posts by Rikard Rönnkvist) and simply put together in a single profile script which I use on all of my Windows boxes:

 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 # Profile script (very heavily) based on the example here:
 # http://www.snowland.se/2010/02/23/nice-powershell-prompt/
 # by Rikard Ronnkvist (www.snowland.se)
 #
 #
 # Script includes a multicolored prompt with marker for sessions started as Admin and marker for providers not filesystem:
 # C:\Windows\System32>
 # [Admin] C:\Windows\System32>
 # [Registry] HKLM:\SOFTWARE\Microsoft\Windows>
 # [Admin] [Registry] HKLM:\SOFTWARE\Microsoft\Windows>
 #
 #
 # Additions made to add command history (stored in the user's profile as \Documents\WindowsPowerShell\commandHistory.xml)
 # http://www.snowland.se/2013/06/14/auto-load-command-history/
 # also by Rikard Ronnkvist (www.snowland.se)
 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


Function Save-CommandHistory
{
 $xmlPath = Join-Path (Split-Path $profile.CurrentUserAllHosts) "commandHistory.xml"
 Get-History | Export-Clixml -Path $xmlPath
}

Function Load-CommandHistory
{
 $xmlPath = Join-Path (Split-Path $profile.CurrentUserAllHosts) "commandHistory.xml"
 Add-History -InputObject (Import-Clixml -Path $xmlPath)
}

Function Prompt
{
 # Create new WindowTitle:
 $Host.UI.RawUI.WindowTitle = "PowerShell v" + (get-host).Version.Major + "." + (get-host).Version.Minor + " (" + $pwd.Provider.Name + ") " + $pwd.Path
 
 # Running as admin?
 if( (
 New-Object Security.Principal.WindowsPrincipal (
 [Security.Principal.WindowsIdentity]::GetCurrent())
 ).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
 {
 # Denote Admin in WindowTitle
 $Host.UI.RawUI.WindowTitle = "[Admin] " + $Host.UI.RawUI.WindowTitle
 
 # Create red "Admin" prompt:
 Write-Host "[" -nonewline -foregroundcolor DarkGray
 Write-Host "Admin" -nonewline -foregroundcolor Red
 Write-Host "] " -nonewline -foregroundcolor DarkGray
 }
 
 # Show ProviderName if outside FileSystem:
 if ($pwd.Provider.Name -ne "FileSystem") {
 Write-Host "[" -nonewline -foregroundcolor DarkGray
 Write-Host $pwd.Provider.Name -nonewline -foregroundcolor Gray
 Write-Host "] " -nonewline -foregroundcolor DarkGray
 }
 
 # Split path and write \ in grey:
 $pwd.Path.Split("\") | foreach {
 Write-Host $_ -nonewline -foregroundcolor Yellow
 Write-Host "\" -nonewline -foregroundcolor Gray
 }
 
 # Backspace last \ and write >:
 Write-Host "`b>" -nonewline -foregroundcolor Gray

 
 # Save command history
 Save-CommandHistory

 return " "
}

#Write-Host "Loading command history..."
Load-CommandHistory

Permanent link to this article: http://www.cluberti.com/blog/2015/09/26/powershell-profile/

Bad Behavior has blocked 251 access attempts in the last 7 days.