Skip to content

Instantly share code, notes, and snippets.

@tormodfj
Last active October 29, 2023 13:38
Show Gist options
  • Save tormodfj/52d917c6e6829ed3fc84b0a89175c4cb to your computer and use it in GitHub Desktop.
Save tormodfj/52d917c6e6829ed3fc84b0a89175c4cb to your computer and use it in GitHub Desktop.
Scripts from my PowerShell talk
function Get-WeatherForecast {
<#
.SYNOPSIS
Gets the weather forecast for a location of choice.
.DESCRIPTION
Displays the weather forecast for a location of choice, directly in
the terminal. Powered by wttr.in.
.NOTES
Author: Tormod Fjeldskår
.EXAMPLE
Get-WeatherForecast Oslo
Shows the weather forecast for Oslo, Norway.
.EXAMPLE
Get-WeatherForecast -Location Stavanger -Wind mps -Days 2
Shows the 2 day weather forecast for Stavanger, Norway with wind
speeds in meters per second.
.PARAMETER Location
The location for which to get the forecast
.PARAMETER Wind
The unit of measure for wind speeds (mps = m/s, kph = km/h)
.PARAMETER Days
The number of days between 0 and 3 for which to get the forecast
.PARAMETER Narrow
If set, only day and night forecasts are provided
.PARAMETER Quiet
If set, no location information is provided
#>
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[ValidatePattern("^[a-zA-Z]+$")]
[string] $Location,
[ValidateSet("mps", "kph")]
[string] $Wind = "mps",
[ValidateRange(0,3)]
[int] $Days = 3,
[switch] $Narrow,
[switch] $Quiet
)
$options = ''
if ($Wind -eq "mps") { $options += "M" }
if ($Wind -eq "kph") { $options += "m" }
if ($Days -eq 0) { $options += "0" }
if ($Days -eq 1) { $options += "1" }
if ($Days -eq 2) { $options += "2" }
if ($Narrow) { $options += "n" }
if ($Quiet) { $options += "Q" }
$url = "http://wttr.in/{0}?{1}" -f $Location, $options
$response = Invoke-WebRequest $url -UserAgent curl
Write-Output $response.Content
}
Import-Module PSColor
Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Agnoster
Install-Module -Scope CurrentUser PSColor
Install-Module -Scope CurrentUser posh-git
Install-Module -Scope CurrentUser oh-my-posh
$gdp = Import-Csv gdp.csv
$gdp | ? { $_."Country Code" -eq "NOR" }
$gdp | select "Country Name", "Country Code", "2018"
$gdp | select "Country Name", "Country Code", {$_."2018" -as [int]}
$gdp | select "Country Name", "Country Code", @{Name="GDP"; Expression={$_."2018" -as [int]}}
$gdp | select "Country Name", "Country Code", @{Name="GDP"; Expression={$_."2018" -as [int]}} | sort "GDP" -Descending
$gdp | select "Country Name", "Country Code", @{Name="GDP"; Expression={$_."2018" -as [int]}} | sort "GDP" -Descending | select -First 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment