dotfiles/PS/PowerShellProfile.ps1

141 lines
3.9 KiB
PowerShell

# 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
)
$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)]
[string[]]
$Source,
[Parameter(Position = 1, Mandatory = $true)]
[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
}
}
}
<#
.Synopsis
Get Powershell Version and optionally the host version.
.Parameter Full
Show all the version information not just the version
number and edition.
.Parameter WithHost
Show the Host version as well.
#>
function Get-PSVersion {
param (
[Parameter(Mandatory = $false)]
[Switch] $Full,
[Parameter(Mandatory = $false)]
[Switch] $WithHost
)
if ($Full) {
$PSVersionTable |
if ($WithHost) {
Get-Host
}
}
else {
@{ PSVersion = $PSVersionTable.PSVersion; PSEdition = $PSVersionTable.PSEdition }
if ($WithHost) {
$HostInfo = Get-Host
@{HostName = $HostInfo.Name; HostVersion = $HostInfo.Version }
}
}
return $output
}
function Get-Base64-Decoded {
[CmdletBinding()]
param (
[Parameter()]
[string[]]
$encoded
)
[Text.Encoding]::Utf8.GetString([Convert]::FromBase64String($encoded))
}
function grep {
$input | out-string -stream | select-string $args
}
# It's way more useful to call this open than ii
Set-Alias -Name open -Value Invoke-Item
Set-Alias -Name ln -Value New-Link
Set-Alias -name which -Value Get-Command
# Add make to our powershell path
$env:PATH = "$env:PATH;C:\Program Files (x86)\GnuWin32\bin;C:\Users\jerem\AppData\Local\Julia-1.3.0\bin;C:\Users\jerem\AppData\Roaming\nvm;C:\Users\jerem\AppData\Local\Google\Cloud SDK\google-cloud-sdk\bin"
$env:NVM_HOME = "C:\Users\jerem\AppData\Roaming\nvm"