Skip to content

Instantly share code, notes, and snippets.

@techthoughts2

techthoughts2/PSProfile

Last active Nov 18, 2020
Embed
What would you like to do?
#region functions
#region functions
<#
.SYNOPSIS
Returns weather report information.
.DESCRIPTION
Console-oriented weather forecast that returns weather information for specified parameters.
.EXAMPLE
Get-Weather
Returns full weather information based on the location of your IP with all defaults.
.EXAMPLE
Get-Weather -Short
Returns basic weather information based on the location of your IP.
.EXAMPLE
Get-Weather -City 'London' -Units Metric -Language 'en'
Returns full weather information for the city of Londa in Metric units with UK language.
.EXAMPLE
Get-Weather -City 'San Antonio' -Units USCS -Short
Returns basic weather information for the city of San Antonio in United State customary units.
.PARAMETER City
The city you would like to get the weather from. If not specified the city of your IP is used.
.PARAMETER Units
Units to display Metric vs United States cusomtary units
.PARAMETER Language
Language to display results in
.PARAMETER Short
Will return only basic weather information
.NOTES
https://github.com/chubin/wttr.in
https://wttr.in/:help
#>
function Get-Weather {
[CmdletBinding()]
param (
[Parameter(
Position = 0,
Mandatory = $false
)]
[string]
$City,
[Parameter(Position = 1)]
[ValidateSet('Metric', 'USCS')]
[string]
$Units = 'USCS',
[Parameter(Position = 2)]
[ValidateSet('ar', 'af', 'be', 'ca', 'da', 'de', 'el', 'en', 'et', 'fr', 'fa', 'hu', 'ia', 'id', 'it', 'nb', 'nl', 'pl', 'pt-br', 'ro', 'ru', 'tr', 'th', 'uk', 'vi', 'zh-cn', 'zh-tw')]
[string]
$Language = 'en',
[Parameter(Position = 3)]
[switch]
$Short
)
$uriString = 'https://wttr.in/'
if ($City) {
$uriString += "$City"
}
switch ($Units) {
'Metric' {
$uriString += "?m"
}
'USCS' {
$uriString += "?u"
}
}
if ($Short) {
$uriString += "&format=4"
}
$uriString += "&lang=$Language"
Write-Verbose "URI: $uriString"
$invokeSplat = @{
Uri = $uriString
ErrorAction = 'Stop'
}
try {
Invoke-RestMethod @invokeSplat
}
catch {
Write-Error $_
}
}#Get-Weather
<#
.SYNOPSIS
Clears all errors, variables, console, and sets locations back to C:\
.NOTES
System variables are not cleared - as they are gathered previously and left un-touched.
#>
function Clear-All {
$sysvariables = $sysvars
#clear all errors
$error.clear()
#clear all non-system variables
Get-Variable |
Where-Object { $sysvariables -notcontains $_.Name -and $_.Name -ne "sysvariables" -and $_.Name -ne 'GitPromptSettings' } |
ForEach-Object { Remove-Variable $_.name -Scope Global }
Clear-Host
Set-Location C:\
}
<#
.SYNOPSIS
Opens explorer window to the current console path location
#>
function Open-Here {
explorer $pwd
}
<#
.SYNOPSIS
Get beautiful syntax for any cmdlet
#>
function Get-Syntax {
[CmdletBinding()]
param (
$Command,
[switch]
$PrettySyntax
)
$check = Get-Command -Name $Command
$params = @{
Name = if ($check.CommandType -eq 'Alias') {
Get-Command -Name $check.Definition
}
else {
$Command
}
Syntax = $true
}
$pretty = $true
if ($pretty -eq $true) {
(Get-Command @params) -replace '(\s(?=\[)|\s(?=-))', "`r`n "
}
else {
Get-Command @params
}
}
<#
.SYNOPSIS
Quickly and easily reverses a list
#>
function Sort-Reverse {
$rank = [int]::MaxValue
$input | Sort-Object { (--(Get-Variable rank -Scope 1).Value) }
}
<#
.SYNOPSIS
Changes directory to main daily working directory
#>
function Set-WorkingDirectory {
Set-Location -Path "$env:USERPROFILE\desktop\project\0_CodeProject"
}
#endregion
#region variables
#Write-Host "Initializing the Shell..." -ForegroundColor DarkCyan -BackgroundColor White
# $env:USERPROFILE\Documents\PowerShell\Modules\oh-my-posh\2.0.342\Themes
# D:\Users\jwm\Documents\PowerShell
$global:sysvars = Get-Variable | Select-Object -ExpandProperty Name
$global:sysvars += 'sysvars'
$greetings =
'Welcome back Jake',
'Glad to see you Jake!',
'Happy coding Jake!',
'Have a great day Jake!',
'Be one with the Snover!',
'May the PowerShell be with you!'
#endregion
#region aliases
# set easy aliases for various functions
New-Alias -Name ca -value Clear-All
New-Alias -Name op -value Open-Here
New-Alias -Name syn -value Get-Syntax
New-Alias -Name sr -value Sort-Reverse
New-Alias -Name work -Value Set-WorkingDirectory
New-Alias -Name gw -Value Get-Weather
#endregion
#region modules
# load up any desired modules
if ($host.Name -eq 'ConsoleHost') {
$modulesToLoad = @(
'posh-git',
'oh-my-posh'
)
}
else {
$modulesToLoad = @(
'posh-git'
)
}
foreach ($module in $modulesToLoad) {
Write-Host -ForegroundColor Gray ("Jake, at your request I am loading up the $module module...")
try {
Import-Module $module -ErrorAction Stop
Write-Host "$module loaded." -ForegroundColor Magenta
}
catch {
Write-Host "Jake, $module has failed to load." -ForegroundColor DarkRed
}
}
#endregion
#region settings
$ProgressPreference = 'SilentlyContinue'
#endregion
[version]$versionReq = '6.0.0.0'
if ($host.Name -eq 'ConsoleHost' -and $host.Version -gt $versionReq) {
# https://www.hanselman.com/blog/PatchingTheNewCascadiaCodeToIncludePowerlineGlyphsAndOtherNerdFontsForTheWindowsTerminal.aspx
# https://www.hanselman.com/blog/HowToMakeAPrettyPromptInWindowsTerminalWithPowerlineNerdFontsCascadiaCodeWSLAndOhmyposh.aspx
Set-Theme Jake
}
elseif ($host.Name -eq 'Visual Studio Code Host') {
Write-Host 'Loading EditorServicesCommandSuite...' -ForegroundColor Gray
if (Get-Module -Name EditorServicesCommandSuite -ListAvailable) {
# Import-Module EditorServicesCommandSuite -ErrorAction SilentlyContinue #workaround
Import-Module EditorServicesCommandSuite
Import-EditorCommand -Module EditorServicesCommandSuite
}
}
else {
# Custom prompt function
function global:prompt {
$color = @{
Reset = "`e[0m"
Red = "`e[31;1m"
Green = "`e[32;1m"
Yellow = "`e[33;1m"
Grey = "`e[37;0m"
White = "`e[37;1m"
Invert = "`e[7m"
RedBackground = "`e[41m"
}
$global:promptDateTime = [datetime]::Now
$Global:promptDate = $global:promptDateTime.ToString("MM-dd-yy")
$Global:promptTime = $global:promptDateTime.ToString("HH:mm")
# truncate the current location if too long
$currentDirectory = $executionContext.SessionState.Path.CurrentLocation.Path
$consoleWidth = [Console]::WindowWidth
$maxPath = [int]($consoleWidth / 4.5)
if ($currentDirectory.Length -gt $maxPath) {
$currentDirectory = "`u{2026}" + $currentDirectory.SubString($currentDirectory.Length - $maxPath)
}
# $global:promptPath = $pwd.ToString().split('\')[-2..-1] -join '\'
$global:promptPath = $currentDirectory
# "${lastCmdTime}${currentDirectory}${gitBranch}${devBuild}`n${lastExit}PS$($color.Reset)$('>' * ($nestedPromptLevel + 1)) "
Write-Host -Object ("[{0} {1}]" -f $global:promptDate, $global:PromptTime) -ForegroundColor Green -NoNewline
(" {0}> " -f $global:promptPath)
Write-VcsStatus
return " "
}
}
#_________________________________________________________________________________________
Set-Location C:\
#_________________________________________________________________________________________
if ($host.Version -gt $versionReq) {
Set-PSReadlineOption -ContinuationPrompt ">>>"
Set-PSReadLineOption -Colors @{Selection = "`e[92;7m" }
Set-PSReadLineOption -PredictionSource History
# Set-PSReadLineOption -Colors @{ InlinePrediction = "$([char]0x1b)[38;5;238m" }
# Set-PSReadLineOption -Colors @{ InlinePrediction = '#8A0303' }
Set-PSReadLineOption -Colors @{ InlinePrediction = '#2F7004' }
# Set-PSReadLineOption -Colors @{ InlinePrediction = "$([char]0x1b)[36;7;238m" }
}
Write-Host "Initilization complete." ($greetings | Get-Random) -ForegroundColor Cyan -BackgroundColor Black
#_________________________________________________________________________________________
Write-Host -ForegroundColor Cyan ($greetings | Get-Random)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.