Skip to content

Instantly share code, notes, and snippets.

@mklement0
mklement0 / PSStylePolyfillForWinPS.ps1
Last active June 26, 2023 21:25
$PSStyle polyfill forWindows PowerShell
# $PSStyle polyfill for Windows PowerShell:
# When run on a PS v7.2+ system, generates source code for Windows PowerShell
# that creates a nested ordered hashtable that mimics the v7.2+ $PSStyle object,
# albeit only with respect to the properties with general-purpose ANSI (VT)
# escape sequences, such as $PSStyle.Italic and $PSStyle.Foreground.Yellow
#
# Simply copy and paste this snippet and execute it in a PowerShell 7.2+ session,
# and it will output source code for Windows PowerShell that defines $PSStyle.
@"
if (`$null -eq `$PSStyle) {
// Content for a Visual Studio Code "keybindings.json" file.
// To open your file for editing, use the command palette's "Preferences: Open Keyboard Shortcuts (JSON)" command.
// CAVEAT: You can copy and paste this file's content as-is to REPLACE your file's existing content
// but in doing so you'll lose any PREEXISTING definitions.
// To preserve any existing definitions, append everything *inside* "[" and "]" below to the *preexisting*
// top-level "[" and "]" enclosure (a JSON array), and be sure to append a "," to the last preexisting object
// inside the array before appending (or place the new objects at the top and append a "," to the second of
// the two new objects).
[
// General definition for all shells *except* PowerShell.
@mklement0
mklement0 / bm_75174855.ps1
Last active June 8, 2023 01:37
PowerShell benchmarks for specific Stack Overflow questions
# Benchmarks for the answers at https://stackoverflow.com/q/75174855/45375.
# Specify how many sample data rows to use (use multiples of 10)
$totalRowCount = 1000
# How many runs to average.
# Note: With values above 15 you'll start to see the effects of on-demand compilation.
$runCount = 10
# Download and define function `Time-Command` on demand (will prompt).
# To be safe, inspect the source code at the specified URL first.
@mklement0
mklement0 / ConvertTo-BodyWithEncoding.ps1
Last active September 7, 2023 18:55
PowerShell function that converts the raw body of a web-request response to a string based on the given character encoding.
<#
Prerequisites: Window PowerShell v5.1 and PowerShell (Core), on all supported platforms. (May work in earlier versions.)
License: MIT
Author: Michael Klement <mklement0@gmail.com>
DOWNLOAD and INSTANT DEFINITION OF THE FUNCTION:
irm https://gist.github.com/mklement0/209a9506b8ba32246f95d1cc238d564d/raw/ConvertTo-BodyWithEncoding.ps1 | iex
@mklement0
mklement0 / Get-WinError.ps1
Last active December 9, 2023 22:32
PowerShell function that looks up information about Windows errors, including HRESULT values, by number or name.
<#
Prerequisites: Windows PowerShell v5.1 or PowerShell Core (v6+)
License: MIT
Author: Michael Klement <mklement0@gmail.com>
DOWNLOAD and INSTANT DEFINITION OF THE FUNCTION:
irm https://gist.github.com/mklement0/0fc086da1af9a72a94cbdb4a59d55230/raw/Get-WinError.ps1 | iex
@mklement0
mklement0 / ConvertTo-IntegerUnchecked.ps1
Last active September 10, 2022 21:27
PowerShell function that converts a given integer to another integer type unchecked, i.e. with overflow allowed.
<#
Prerequisites: Windows PowerShell v5.1 (possibly earlier; untested) or PowerShell (Core)
License: MIT
Author: Michael Klement <mklement0@gmail.com>
DOWNLOAD and INSTANT DEFINITION OF THE FUNCTION:
irm https://gist.github.com/mklement0/a700e598b615a1d432cc87e7291cdfec/raw/ConvertTo-IntegerUnchecked.ps1 | iex
#requires -Version 7
# Download and define function `Time-Command` on demand (will prompt).
# To be safe, inspect the source code at the specified URL first.
if (-not (Get-Command -ErrorAction Ignore Time-Command)) {
$gistUrl = 'https://gist.github.com/mklement0/9e1f13978620b09ab2d15da5535d1b27/raw/Time-Command.ps1'
if ((Read-Host "`n====`n OK to download and define benchmark function ``Time-Command```n from Gist ${gistUrl}?`n=====`n(y/n)?").Trim() -notin 'y', 'yes') { Write-Warning 'Aborted.'; exit 2 }
Invoke-RestMethod $gistUrl | Invoke-Expression 3>$null
if (-not ${function:Time-Command}) { exit 2 }
}
@mklement0
mklement0 / Add-NuGetType.ps1
Last active January 10, 2024 18:59
PowerShell function for experimenting with loading .NET assemblies from NuGet packages that are downloaded and cached on demand
<#
Prerequisites: PowerShell v5.1 and above (verified; may also work in earlier versions)
License: MIT
Author: Michael Klement <mklement0@gmail.com>
DOWNLOAD and DEFINITION OF THE FUNCTION:
irm https://gist.github.com/mklement0/7436c9e4b2f73d7256498f959f0d5a7c/raw/Add-NuGetType.ps1 | iex
@mklement0
mklement0 / Show-Help.ps1
Last active October 15, 2023 20:14
Wrapper PowerShell function for Get-Help that shows help topics online by default and supports copying URLs of / Markdown links to the online help topics to the clipboard
<#
Prerequisites: PowerShell v5.1 and above (verified; may also work in earlier versions)
License: MIT
Author: Michael Klement <mklement0@gmail.com>
DOWNLOAD and DEFINITION OF THE FUNCTION:
irm https://gist.github.com/mklement0/880624fd665073bb439dfff5d71da886/raw/Show-Help.ps1 | iex
@mklement0
mklement0 / PSReadLineMetaInfoKeyHandler.ps1
Last active April 26, 2021 13:11
PowerShell sample code that demonstrates a PSReadLine key handler that display meta-information about the command being typed in real time.
<#
License: MIT
Author: Michael Klement <mklement0@gmail.com>
See https://stackoverflow.com/a/67266971/45375 for more information.
#>
# The printable characters to respond to.