Skip to content

Instantly share code, notes, and snippets.

@Razzile
Last active November 2, 2019 13:03
Show Gist options
  • Save Razzile/f7c11dfd98fcb32fcdb9d5449c2a7fc6 to your computer and use it in GitHub Desktop.
Save Razzile/f7c11dfd98fcb32fcdb9d5449c2a7fc6 to your computer and use it in GitHub Desktop.
My powershell configuration
# Use this file to run your own startup commands
## Prompt Customization
<#
.SYNTAX
<PrePrompt><CMDER DEFAULT>
λ <PostPrompt> <repl input>
.EXAMPLE
<PrePrompt>N:\Documents\src\cmder [master]
λ <PostPrompt> |
#>
#Import chocolatey helpers
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
[ScriptBlock]$PrePrompt = {
}
# Replace the cmder prompt entirely with this.
[ScriptBlock]$CmderPrompt = {
$Host.UI.RawUI.ForegroundColor = "Gray"
# Workaround to make above line apply the "White" foreground color.
# Seems like you have to print _something_ before using Write-Host with -ForegroundColor.
# Note: Empty string "" doesn't work.
Write-Host "`r" -NoNewline
Microsoft.PowerShell.Utility\Write-Host $pwd.ProviderPath -NoNewLine -ForegroundColor Green
Microsoft.PowerShell.Utility\Write-Host (checkGit($pwd.ProviderPath)) -NoNewLine
}
[ScriptBlock]$PostPrompt = {
}
## <Continue to add your own>
<#
Launch-App launches an app with a name that matches the provided string. wildcards are supported
#>
function Launch-App {
param(
[parameter(mandatory = $true)][ValidateNotNullOrEmpty()][string[]]$appname,
[switch] $admin
)
$apps = (New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items()
if ($apps) {
$count = 0
foreach ($app in $apps) {
if ($app.Name -like $appname) {
$count++
if ($admin) {
#"Run as administrator" action
$action = $app.Verbs() | ? { $_.Name.replace('&', '') -eq "Run as administrator" }
}
else {
# "Open" action
$action = $app.Verbs() | ? { $_.Name.replace('&', '') -eq "Open" }
}
if ($action) {
$action.DoIt()
}
else {
Write-Error "Application '$appname' doesn't support this action."
}
}
}
if ($count -eq 0) {
Write-Error "Application '$appname' Not Found."
}
}
else {
Write-Error "An unexpected error has occurred."
}
}
# Try load visual studio environment variables by default
function Import-VSEnvironment {
Write-Output "Setting up Visual Studio Developer Environment..."
if (Get-Command "vswhere.exe" -errorAction SilentlyContinue) {
$installationPath = vswhere.exe -latest -property installationPath
if ($installationPath -and (Test-Path "$installationPath\Common7\Tools\vsdevcmd.bat")) {
$json = $(& "${env:COMSPEC}" /s /c "`"$installationPath\Common7\Tools\vsdevcmd.bat`" -no_logo -arch=x64 && powershell -Command `"Get-ChildItem env: | Select-Object Key,Value | ConvertTo-Json`"")
if ($LASTEXITCODE -ne 0) {
Write-Error "($LASTEXITCODE) $installationPath\Common7\Tools\vsdevcmd.bat: $json"
}
else {
# Write-Host $json
$($json | ConvertFrom-Json) | ForEach-Object {
$k, $v = $_.Key, $_.Value
Set-Content env:\"$k" "$v"
}
}
}
else {
Write-Error "Cannot find 'vsdevcmd.bat' in installation directory [$installationPath\Common7\Tools\vsdevcmd.bat]"
}
}
else {
Write-Error "Executable 'vswhere' not found in PATH"
}
}
function Create-File {
param ([parameter(mandatory = $true)][ValidateNotNullOrEmpty()][string[]]$name)
New-Item $name -type file
}
# set an alias for Launch-App
Set-Alias -Name launch -Value Launch-App
# set an alias for Import-VSEnvironment
Set-Alias -Name vsenv -Value Import-VSEnvironment
# set an alias for Create-File
Set-Alias -Name new -Value Create-File
# use proj variable as shorthand for $env:proj
$proj = $env:proj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment