Skip to content

Instantly share code, notes, and snippets.

@joshrobb
Forked from bradwilson/My.Types.ps1xml
Created May 3, 2012 03:13
Show Gist options
  • Save joshrobb/2582758 to your computer and use it in GitHub Desktop.
Save joshrobb/2582758 to your computer and use it in GitHub Desktop.
My profile.ps1 (and several related scripts)
if ($GLOBAL:PWD -isnot [System.Management.Automation.PathInfo])
{
$GLOBAL:PWD = get-location
}
if ($GLOBAL:CDHIST -isnot [System.Collections.ArrayList])
{
$GLOBAL:CDHIST = [System.Collections.Arraylist]::Repeat($PWD, 1)
}
$cwd = get-location
$count = $GLOBAL:CDHIST.count
if ($args.length -eq 0)
{
set-location $HOME
$GLOBAL:PWD = get-location
$GLOBAL:CDHIST.Remove($GLOBAL:PWD)
if ($GLOBAL:CDHIST[0] -ne $GLOBAL:PWD)
{
$GLOBAL:CDHIST.Insert(0,$GLOBAL:PWD)
}
}
elseif ($args[0] -like "-[0-9]*")
{
$num = $args[0].Replace("-","")
$GLOBAL:PWD = $GLOBAL:CDHIST[$num]
set-location $GLOBAL:PWD
$GLOBAL:CDHIST.RemoveAt($num)
$GLOBAL:CDHIST.Insert(0,$GLOBAL:PWD)
}
elseif ($args[0] -eq "-l")
{
$start = [System.Math]::Max(0, $count - 50)
for ($i = 0; $i -lt $count; $i++)
{
"{0,6} {1}" -f $i, $GLOBAL:CDHIST[$i].ToString().Replace("Microsoft.PowerShell.Core\FileSystem::","")
}
}
elseif ($args[0] -eq "-")
{
if ($GLOBAL:CDHIST.count -gt 1)
{
$t = $CDHIST[0]
$CDHIST[0] = $CDHIST[1]
$CDHIST[1] = $t
set-location $GLOBAL:CDHIST[0]
$GLOBAL:PWD = get-location
}
}
else
{
set-location "$args"
$GLOBAL:PWD = get-location
for ($i = $count - 1; $i -ge 0; $i--)
{
if ($GLOBAL:PWD.Path -eq $GLOBAL:CDHIST[$i])
{
$GLOBAL:CDHIST.RemoveAt($i)
}
}
$GLOBAL:CDHIST.Insert($GLOBAL:CDHIST.count, $GLOBAL:PWD)
}
$GLOBAL:PWD = get-location
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$windowsPrincipal = new-object 'System.Security.Principal.WindowsPrincipal' $windowsIdentity
return $windowsPrincipal.IsInRole("Administrators")
$host.UI.RawUI.WindowTitle
<Types>
<Type>
<Name>System.String</Name>
<Members>
<ScriptProperty>
<Name>ToBase64</Name>
<GetScriptBlock>
[System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($this))
</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>FromBase64</Name>
<GetScriptBlock>
[System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($this))
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
</Types>
# Sometimes home doesn't get properly set for pre-Vista LUA-style elevated admins
if ($home -eq "") {
remove-item -force variable:\home
$home = (get-content env:\USERPROFILE)
(get-psprovider 'FileSystem').Home = $home
}
set-content env:\HOME $home
# Keep the existing window title
$windowTitle = (get-title).Trim()
if ($windowTitle.StartsWith("Administrator:")) {
$windowTitle = $windowTitle.Substring(14).Trim()
}
# Posh-Git overrides
$GitPromptSettings.AfterBackgroundColor = "DarkRed"
$GitPromptSettings.BeforeBackgroundColor = "DarkRed"
$GitPromptSettings.BeforeIndexBackgroundColor = "DarkRed"
$GitPromptSettings.BranchBackgroundColor = "DarkRed"
$GitPromptSettings.BranchAheadBackgroundColor = "DarkRed"
$GitPromptSettings.BranchBehindBackgroundColor = "DarkRed"
$GitPromptSettings.BranchBehindAndAheadBackgroundColor = "DarkRed"
$GitPromptSettings.DelimBackgroundColor = "DarkRed"
$GitPromptSettings.IndexBackgroundColor = "DarkRed"
$GitPromptSettings.WorkingBackgroundColor = "DarkRed"
$GitPromptSettings.UntrackedBackgroundColor = "DarkRed"
$GitPromptSettings.AfterForegroundColor = $Host.UI.RawUI.ForegroundColor
$GitPromptSettings.BeforeForegroundColor = $Host.UI.RawUI.ForegroundColor
$GitPromptSettings.BeforeIndexForegroundColor = "Green"
$GitPromptSettings.BranchForegroundColor = "White"
$GitPromptSettings.IndexForegroundColor = "Green"
$GitPromptSettings.WorkingForegroundColor = "Cyan"
$GitPromptSettings.UntrackedForegroundColor = "Cyan"
$GitPromptSettings.AfterText = " "
$GitPromptSettings.BeforeText = " "
$GitPromptSettings.ShowStatusWhenZero = $false
# Type overrides (starters compliments of Scott Hanselman)
Update-TypeData (join-path $scripts "My.Types.ps1xml")
# Remove default things we don't want
if (test-path alias:\clear) { remove-item -force alias:\clear } # We override with clear.ps1
if (test-path alias:\ri) { remove-item -force alias:\ri } # ri conflicts with Ruby
if (test-path alias:\cd) { remove-item -force alias:\cd } # We override with cd.ps1
if (test-path alias:\chdir) { remove-item -force alias:\chdir } # We override with an alias to cd.ps1
if (test-path alias:\md) { remove-item -force alias:\md } # We override with md.ps1
if (test-path alias:\sc) { remove-item -force alias:\sc } # Conflicts with \Windows\System32\sc.exe
if (test-path function:\md) { remove-item -force function:\md } # We override with md.ps1
if (test-path function:\mkdir) { remove-item -force function:\mkdir } # We override with an alias to md.ps1
if (test-path function:\prompt) { remove-item -force function:\prompt } # We override with prompt.ps1
# Aliases/functions
set-alias grep select-string
set-alias wide format-wide
set-alias whoami get-username
set-alias chdir cd
set-alias mkdir md
set-content function:\mklink "cmd /c mklink `$args"
# Development overrides
set-content env:\TERM "msys" # To shut up Git 1.7.10+
if (test-path "C:\Dev\bin\Startup.ps1") { . C:\Dev\bin\Startup.ps1 }
elseif (test-path "D:\Dev\bin\Startup.ps1") { . D:\Dev\bin\Startup.ps1 }
elseif (test-path "E:\Dev\bin\Startup.ps1") { . E:\Dev\bin\Startup.ps1 }
$nextId = (get-history -count 1).Id + 1;
$currentPath = (get-location).Path.replace($home, "~")
$idx = $currentPath.IndexOf("::")
if ($idx -gt -1) { $currentPath = $currentPath.Substring($idx + 2) }
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$windowsPrincipal = new-object 'System.Security.Principal.WindowsPrincipal' $windowsIdentity
$title = $currentPath
if ($windowTitle -ne $null)
{
$title = ($title + " » " + $windowTitle)
}
if ($psISE)
{
$color = "Black";
}
elseif ($windowsPrincipal.IsInRole("Administrators") -eq 1)
{
$color = "Yellow";
}
else
{
$color = "Green";
}
Write-HgStatus (Get-HgStatus)
Write-GitStatus (Get-GitStatus)
write-host (" [" + $nextId + "]") -NoNewLine -ForegroundColor $color
if ((get-location -stack).Count -gt 0) { write-host ("+" * ((get-location -stack).Count)) -NoNewLine -ForegroundColor Cyan }
set-title $title
return " "
$host.UI.RawUI.WindowTitle = $args[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment