Skip to content

Instantly share code, notes, and snippets.

@kneeprayer
Last active July 16, 2019 02:01
Show Gist options
  • Save kneeprayer/835f8b1a5871bd8d1efff223a8274697 to your computer and use it in GitHub Desktop.
Save kneeprayer/835f8b1a5871bd8d1efff223a8274697 to your computer and use it in GitHub Desktop.
powershell profile
# inspired by yht0827
# https://gist.github.com/yht0827/911fa41750251f66d10650a0cf5a9c20
#
# Before Set this file.
# 1. install ConsoleExtensions Module
# Install-Module -Name Communary.ConsoleExtensions
# details https://www.powershellgallery.com/packages/Communary.ConsoleExtensions/1.0.69
#
# 2. Download Test-IsAdmin and Copy to C:\Windows\System32\WindowsPowerShell\v1.0
# https://gallery.technet.microsoft.com/scriptcenter/1b5df952-9e10-470f-ad7c-dc2bdc2ac946
#
# Finally, copy this file to C:\Windows\System32\WindowsPowerShell\v1.0
# chcp 65001 | Out-Null
$OutputEncoding = [Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8
# Set PowerlinePrompt from ConsoleExtensions
Set-PowerlinePrompt
# Reset Symbol Char (only for Shift-JIS code page on powershell console)
#$global:branchSymbol = "⎇"
#$global:dividerSymbolFull = "▶"
#$global:arrowUpSymbol = "↑"
#$global:arrowDownSymbol = "↓"
# Read Test-IsAdmin
. C:\Windows\System32\WindowsPowerShell\v1.0\Test-IsAdmin.ps1
Set-Alias -Name ll -Value get-childitem
cd ~
function touch {
[CmdletBinding()]
param(
[parameter(
position = 0,
mandatory = 1,
ValueFromPipeline = 1,
ValueFromPipelineByPropertyName = 1
)]
[string]$path,
[parameter(
position = 1,
mandatory = 0,
ValueFromPipeline = 1,
ValueFromPipelineByPropertyName = 1
)]
[datetime]$date = $(Get-Date),
[parameter(
position = 2,
mandatory = 0,
HelpMessage = "Change Last AccessTime only"
)]
[switch]$access,
[parameter(
position = 3,
mandatory = 0,
HelpMessage = "Do not create file if not exist"
)]
[switch]$nocreate,
[parameter(
position = 4,
mandatory = 0,
HelpMessage = "Change Last WriteTime only"
)]
[switch]$modify,
[parameter(
position = 5,
mandatory = 0,
HelpMessage = "LastAccessTime reference file"
)]
[string]$reference
)
if (-not(Test-Path $path)) {
if ((!$nocreate)) {
New-Item -Path $path -ItemType file -Force
}
}
else {
try {
if ($reference) {
$date = (Get-ItemProperty -Path $reference).LastAccessTime
}
if ($access) {
Get-ChildItem $path | % { Set-ItemProperty -path $_.FullName -Name LastAccessTime -Value $date -Force -ErrorAction Stop }
}
if ($modify) {
Get-ChildItem $path | % { Set-ItemProperty -path $_.FullName -Name LastWriteTime -Value $date -Force -ErrorAction Stop }
}
if (!$access -and !$modify) {
Get-ChildItem $path | % { Set-ItemProperty -path $_.FullName -Name LastAccessTime -Value $date -Force -ErrorAction Stop }
Get-ChildItem $path | % { Set-ItemProperty -path $_.FullName -Name LastWriteTime -Value $date -Force -ErrorAction Stop }
}
}
catch {
throw $_
}
finally {
Get-ChildItem $path | % { Get-ItemProperty -Path $_.FullName | select Fullname, LastAccessTime, LastWriteTime }
}
}
}
function which($name) {
Get-Command $name | Select-Object -ExpandProperty Definition
}
$env:TERM = 'cygwin'
$env:TERM = 'FRSX'
$global:foregroundColor = 'white'
$time = Get-Date
$curUser = (Get-ChildItem Env:\USERNAME).Value
Write-Host "Hello, $curUser! " -foregroundColor $foregroundColor -NoNewLine; Write-Host "$([char]9829) " -foregroundColor Red
Write-Host "Today is: $($time.ToLongDateString())"
function Prompt {
# Prompt Colors
# Black DarkBlue DarkGreen DarkCyan DarkRed DarkMagenta DarkYellow
# Gray DarkGray Blue Green Cyan Red Magenta Yellow White
$prompt_text = "White"
$prompt_background = "DarkMagenta"
$prompt_git_background = "Yellow"
$prompt_git_text = "Black"
# Grab Git Branch
$git_string = "";
git branch | foreach {
if ($_ -match "^\* (.*)") {
$git_string += $matches[1]
}
}
# Grab Git Status
$git_status = "";
git status --porcelain | foreach {
$git_status = $_ #just replace other wise it will be empty
}
if (!$git_string) {
$prompt_text = "White"
$prompt_background = "DarkMagenta"
}
if ($git_status) {
$prompt_git_background = "Green"
}
$curtime = Get-Date
$path = $executionContext.SessionState.Path.CurrentLocation.Path
$p = $path
while ($p -ne "") {
if ($p -eq $HOME) { $path = $path.Replace($HOME, " ~"); break }
$p = Split-Path $p
}
# over 45% of battery charge
if ((Get-WmiObject win32_battery).estimatedChargeRemaining -ge 45) {
Write-Host "${global:dividerSymbolFull}" -foregroundColor "DarkGreen" -backgroundColor "DarkGreen" -NoNewLine
Write-Host (Get-WmiObject win32_battery).estimatedChargeRemaining -foregroundColor "Black" -backgroundColor "DarkGreen" -NoNewLine
Write-Host "% " -foregroundColor "Black" -backgroundColor "DarkGreen" -NoNewLine
Write-Host "${global:dividerSymbolFull}" -foregroundColor "DarkGreen" -backgroundColor "Blue" -NoNewLine
}
else {
Write-Host "${global:dividerSymbolFull}" -foregroundColor "DarkRed" -backgroundColor "DarkRed" -NoNewLine
Write-Host (Get-WmiObject win32_battery).estimatedChargeRemaining -foregroundColor "Black" -backgroundColor "DarkRed" -NoNewLine
Write-Host "% " -foregroundColor "Black" -backgroundColor "DarkRed" -NoNewLine
Write-Host "${global:dividerSymbolFull}" -foregroundColor "DarkRed" -backgroundColor "Blue" -NoNewLine
}
Write-Host " $((date).tostring("G")) " -foregroundColor "White" -backgroundColor "Blue" -NoNewLine
Write-Host "${global:dividerSymbolFull}" -foregroundColor "Blue" -backgroundColor $prompt_background -NoNewLine
if ($executionContext.SessionState.Path.CurrentLocation.Path.length -ge 11 -and $executionContext.SessionState.Path.CurrentLocation.Path.Substring(0, 11) -eq "C:\Users\$curUser") {
Write-Host $path.split('\')[0] -foregroundColor $prompt_text -backgroundColor $prompt_background -NoNewLine
for ($i = 3; $i -lt $executionContext.SessionState.Path.CurrentLocation.Path.split('\').count; $i++) {
Write-Host "/" -foregroundColor $prompt_text -backgroundColor $prompt_background -NoNewLine
Write-Host $executionContext.SessionState.Path.CurrentLocation.Path.split('\')[$i] -foregroundColor $prompt_text -backgroundColor $prompt_background -NoNewLine
}
}
else {
if ($executionContext.SessionState.Path.CurrentLocation.Path -eq "C:\") {
Write-Host $executionContext.SessionState.Path.CurrentLocation.Path.split('\')[0] -foregroundColor $prompt_text -backgroundColor $prompt_background -NoNewLine
}
else {
Write-Host $executionContext.SessionState.Path.CurrentLocation.Path.split('\')[0] -foregroundColor $prompt_text -backgroundColor $prompt_background -NoNewLine
Write-Host "/" -foregroundColor $prompt_text -backgroundColor $prompt_background -NoNewLine
Write-Host $executionContext.SessionState.Path.CurrentLocation.Path.split('\')[1] -foregroundColor $prompt_text -backgroundColor $prompt_background -NoNewLine
for ($i = 2; $i -lt $executionContext.SessionState.Path.CurrentLocation.Path.split('\').count; $i++) {
Write-Host "/" -foregroundColor $prompt_text -backgroundColor $prompt_background -NoNewLine
Write-Host $executionContext.SessionState.Path.CurrentLocation.Path.split('\')[$i] -foregroundColor $prompt_text -backgroundColor $prompt_background -NoNewLine
}
}
}
if ($git_string) {
Write-Host "${global:dividerSymbolFull}" -foregroundColor $prompt_background -NoNewLine -backgroundColor $prompt_git_background
Write-Host " ${global:branchSymbol} " -foregroundColor $prompt_git_text -backgroundColor $prompt_git_background -NoNewLine
Write-Host ($git_string) -NoNewLine -foregroundColor $prompt_git_text -backgroundColor $prompt_git_background
Write-Host "${global:dividerSymbolFull}" -foregroundColor $prompt_git_background -NoNewLine
}
else {
Write-Host "${global:dividerSymbolFull}" -foregroundColor $prompt_background -NoNewLine
}
$host.UI.RawUI.WindowTitle = "Current DIR: $path"
Return " "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment