Skip to content

Instantly share code, notes, and snippets.

View Jaykul's full-sized avatar
😀
Learning

Joel Bennett Jaykul

😀
Learning
View GitHub Profile
@Jaykul
Jaykul / Invoke-WithEncoding.ps1
Last active February 22, 2023 07:37
Work around binaries that don't respect the console code page
class EncodingAttribute : System.Management.Automation.ArgumentTransformationAttribute {
[object] Transform([System.Management.Automation.EngineIntrinsics]$engineIntrinsics, [object]$inputData) {
return [System.Text.Encoding]::GetEncoding("$inputData")
}
}
function Invoke-WithEncoding {
<#
.SYNOPSIS
Temporarily switch output encoding
@Jaykul
Jaykul / WslInstall.psm1
Last active January 30, 2026 18:53
Install WSL2 distros non-interactively
function Add-WslUser {
<#
.SYNOPSIS
Adds a user to a WSL distro
#>
[CmdletBinding()]
param(
# The distro to add the user to
[Parameter(Mandatory)]
$Distribution,
# Disable shell integration when the language mode is restricted
# Prevent installing more than once per session
if ($ExecutionContext.SessionState.LanguageMode -ne "FullLanguage" -or (Test-Path variable:global:__VSCodeOriginalPrompt)) {
return;
}
$Global:__VSCodeOriginalPrompt = $function:Prompt
function global:prompt {
$ExitCode = $? ? 0 : ($LASTEXITCODE -ne 0) ? $LASTEXITCODE : 1
@(
@Jaykul
Jaykul / AudioPlaybackEngine.cs
Last active October 8, 2022 05:41 — forked from markheath/AudioPlaybackEngine.cs
FireAndForget NAudio Sample
using System;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
namespace FireAndForgetAudioSample
{
class AudioPlaybackEngine : IDisposable
{
private readonly IWavePlayer outputDevice;
private readonly MixingSampleProvider mixer;
class PSEquality : System.Collections.Generic.EqualityComparer[PSObject] {
[scriptblock]$Comparer = { $_ }
[bool] Equals([PSObject]$first, [PSObject]$other) {
return ($first | ForEach-Object $this.Comparer) -eq ($other | ForEach-Object $this.Comparer)
}
[int] GetHashCode([PSObject]$module) {
return $module.GetHashCode()
}
@Jaykul
Jaykul / Lessons Learned.md
Last active July 13, 2022 06:17
Trying to find out what's the fastest way to count characters ...

The main thing I re-learned doing this is: the more you can do in compiled C# code, the faster it will run. Writing a for-loop in C# is absolutely the fastest way to iterate over characters -- even when you have to call it as a static method.

This means that the built-in .Split(<char>) method is the fastest way to count a specific character (not necessarily the most memory efficient).

Here's the results on my local computer:

Average          Command
-------          -------
00:00:00.0000084 &lt;# hardcoded value shows overhead #&gt; 4
@Jaykul
Jaykul / Show-Date.ps1
Created July 9, 2022 02:59
Show-Date
function Show-Date {
<#
.SYNOPSIS
Get the time span elapsed during the execution of command (by default the previous command)
.DESCRIPTION
Calls Get-History to return a single command and returns the difference between the Start and End execution time
#>
[OutputType([string])]
[CmdletBinding(DefaultParameterSetName = "SimpleFormat")]
param(
function Get-Date {
<#
.SYNOPSIS
Get the time span elapsed during the execution of command (by default the previous command)
.DESCRIPTION
Calls Get-History to return a single command and returns the difference between the Start and End execution time
#>
[OutputType([string])]
[CmdletBinding(DefaultParameterSetName = "SimpleFormat")]
param(
@Jaykul
Jaykul / Computers.csv
Last active June 25, 2022 22:48
An Invoke Wrapper For Labs
ComputerName Email UserName Password
NotOne user@gmail.com bob s3cr3t
NotTwo user2@gmail.com sue m04rs3cr3t
@Jaykul
Jaykul / About joining strings.md
Last active June 28, 2022 02:07
StringBuilder vs += vs -join @()

TL;DR: Use StringBuilder.

The truth: unless you're joining large amounts of long strings, the time it'll take PowerShell to read, parse, and compile the file is going to outweigh any improvements you make in the runtime unless you run the code repeatedly. This is easy to forget about, but try it for yourself. Download and run Strings.ps1 below, and then, run it a second time -- remember, you incur that first run penalty in each new PowerShell session.

image

For dozens to hundreds of strings, StringBuilder is only microseconds faster than +=

Obviously the results vary depending on your strings! The more there are, and the longer they are, the bigger gain you get from using StringBuilder. To sum up: StringBuilder is faster except in very small test cases, but it's not much faster except in extremely large test cases.