dotfiles/PS/PowerShellProfile.ps1

91 lines
2.6 KiB
PowerShell
Raw Normal View History

# My powershell profile.
# customize the prompt to be a little more useful
function prompt {
Write-Host -NoNewLine "["
Write-Host -NoNewLine -Fore Green ("{0:HH}:{0:mm}" -f $(get-date))
Write-Host -NoNewLine "] "
Write-Host -Fore Cyan "${pwd}"
"PS> "
}
<#
.Synopsis
Invoke a scriptblock with evelvated permissions.
#>
function Invoke-Elevated {
param (
[ScriptBlock]
$code
2020-02-09 07:44:46 -06:00
)
$outTmpFile = [System.IO.Path]::GetTempFileName()
$errTmpFile = [System.IO.Path]::GetTempFileName()
$code | Write-Output
$outTmpFile | Write-Output
$errTmpFile | Write-Output
# TODO(jwall): Work on figuring out how to properly redirect output here
Start-Process powershell.exe -ArgumentList { &$code 1>$outTmpFile 2>$errTmpFile } -WindowStyle hidden -Verb RunAs
Get-Content $outTmpFile | Write-Output
Get-Content $errTmpFile | Write-Output
Remove-Item $outTmpFile
Remove-Item $errTmpFile
}
<#
.Synopsis
Returns true if you are running with elevated privileges.
#>
function Get-Elevated {
([Security.Principal.WindowsPrincipal] `
[Security.Principal.WindowsIdentity]::GetCurrent() `
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
<#
.Synopsis
Create a symlink from a source to a destination.
.Description
Creates a symlink from a $Source to a $Destination.
Automatically elevates to administrator privileges
if necessary.
.Parameter Source
Required source to create the symlink from.
.Parameter Destination
Required destination to create the symlink at.
#>
function New-Link {
param (
[Parameter(Position=0, Mandatory=$true)]
2020-02-09 07:44:46 -06:00
[string[]]
$Source,
[Parameter(Position=1, Mandatory=$true)]
2020-02-09 07:44:46 -06:00
[string[]]
$Destination
)
# This is a bit of a hack but it's necessary until they make it possible
# to do symbolic links without administrator privileges
if (Get-Elevated) {
New-Item -ItemType SymbolicLink -Path $Destination -Target $Source
} else {
# Elevate our permissions for this first.
Invoke-Elevated {
New-Item -ItemType SymbolicLink -Path $Destination -Target $Source
}
}
2019-10-26 14:59:31 -05:00
}
2020-02-09 07:44:46 -06:00
function grep {
$input | out-string -stream | select-string $args
}
2019-10-26 14:59:31 -05:00
# It's way more useful to call this open than ii
2019-11-09 19:52:12 -06:00
Set-Alias -Name open -Value Invoke-Item
2020-02-09 07:44:46 -06:00
Set-Alias -Name ln -Value New-Link
2019-11-09 19:52:12 -06:00
# Add make to our powershell path
2020-02-09 07:44:46 -06:00
$env:PATH="$env:PATH;C:\Program Files (x86)\GnuWin32\bin;C:\Users\jerem\AppData\Local\Julia-1.3.0\bin"