Skip to content

Instantly share code, notes, and snippets.

@mklement0
mklement0 / Select-StringAll.ps1
Last active April 24, 2021 13:19
PowerShell function that wraps the Select-String cmdlet with conjunctive matching (ALL search patterns must match).
<#
Prerequisites: PowerShell v3+
License: MIT
Author: Michael Klement <mklement0@gmail.com>
DOWNLOAD and INSTANT DEFINITION OF THE FUNCTION:
irm https://gist.github.com/mklement0/356acffc2521fdd338ef9d6daf41ef07/raw/Select-StringAll.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.
@mklement0
mklement0 / Select-StringFormatted.ps1
Last active April 28, 2021 23:06
PowerShell function that wraps Select-String to make it search the formatted representations of its input objects.
<#
Prerequisites: PowerShell v3+
License: MIT
Author: Michael Klement <mklement0@gmail.com>
DOWNLOAD and DEFINITION OF THE FUNCTION:
irm https://gist.github.com/mklement0/46fea9e6e5ef1a3ceaf681c976cb68e3/raw/Select-StringFormatted.ps1 | iex
@mklement0
mklement0 / Get-InstalledProgram.ps1
Last active August 9, 2021 16:19
Windows PowerShell script that gets information about installed programs (applications), via the registry by default, optionally via the PackageManagement module
<#
.SYNOPSIS
Gets installed programs on Windows.
.DESCRIPTION
Gets installed programs (applications) on Windows, either via the registry or, optionally,
via the Get-Package cmdlet, optionally filtered by
display-name substrings.
NOTE: As of PowerShell, v7.2, -UsePackageManagement only works in Windows PowerShell
(with the PackageManagement module installed).
@mklement0
mklement0 / Show-OperatorHelp.ps1
Last active September 24, 2021 20:52
PowerShell function for performing online documentation lookups for PowerShell's operators and operator-like symbols - see https://stackoverflow.com/a/59324892/45375
<#
Prerequisites: PowerShell v3+
License: MIT
Author: Michael Klement <mklement0@gmail.com>
DOWNLOAD and DEFINITION OF THE FUNCTION:
irm https://gist.github.com/mklement0/146f3202a810a74cb54a2d353ee4003f/raw/Show-OperatorHelp.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 / New-EncodingTestFiles.ps1
Last active August 16, 2022 08:18
PowerShell scripts for creating and reading test files with the standard Unicode character encoding schemes and default encodings.
<#
.SYNOPSIS
Creates test text-based files with various character encodings.
.DESCRIPTION
Creates text-based test files using
* all 5 byte order-marked Unicode character encoding schemes,
both with and without BOM (Unicode signature)
* default encodings,
with the the platform's default encoding, [System.Text.Encoding]::Default
@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
@mklement0
mklement0 / wrapper.cmd
Last active December 6, 2022 01:35
wrapper.cmd - a wrapper batch-file template for executing embedded PowerShell and/or VBScript/JScript code
@echo off
setLocal enableDelayedExpansion
:: === ADAPTING THIS TEMPLATE (for help, see bottom or invoke with "help") ===
:: * Step 1 of 3: CLONE THIS BATCH FILE and give it a name of your choice.
:: * Step 2 of 3: Set the TARGET LANGUAGE on the next line.
:: One of: "ps1" "vbs" "js" (PowerShell, VBScript, JScript)
:: OR: "all" (runs ALL embedded snippets, in sequence)
:: NOTE: To add support for a new language, search for "NEW LANGUAGE"
:: below and follow the instructions there.
set "WRAPPER_LANG=ps1"
@mklement0
mklement0 / Out-FileUtf8NoBom.ps1
Last active December 23, 2022 15:30
PowerShell function that emulates Out-File for creating UTF-8-encoded files *without a BOM* (byte-order mark).
<#
Prerequisites: PowerShell version 3 or above.
License: MIT
Author: Michael Klement <mklement0@gmail.com>
DOWNLOAD and DEFINITION OF THE FUNCTION:
irm https://gist.github.com/mklement0/8689b9b5123a9ba11df7214f82a673be/raw/Out-FileUtf8NoBom.ps1 | iex
The above directly defines the function below in your session and offers guidance for making it available in future