Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
ninmonkey / PowerQuery - Nested Let vs Record Expression.md
Last active February 21, 2025 16:05
Let expressions are synactic sugar for record expressions

I used pseudocode to shorten the example. The structure is good.

Using nested let

let
    Source1 =
        let 
            Workbook = File.Contents(...),
            Final    = Table.SelectColumns( ... )
@ninmonkey
ninmonkey / Pwsh▸ParameterIsBlank▸NewEmployee.ps1
Last active February 3, 2025 15:12
Pwsh▸ParameterIsBlank▸NewEmployee.ps1
function NewUser {
<#
.synopsis
Create a new user with a name. Any "blank" values will fallback with a default value
.DESCRIPTION
If you use ValidateIfNotSomething, invalid or missing values throws.
- Sometimes you want the command that always works, with a fallback value
- $null, empty string to count as blank
@ninmonkey
ninmonkey / Find Archived CDC Files without Javascript.ps1
Created February 1, 2025 17:50
Find archived urls. This method requires no javascript/session/headless browser, etc. 2025/01
using namespace System.Collections.Generic
<#
Using: Pwsh 7
Related:
For parsing HTML with CSS Selector queries, see this template: <https://gist.github.com/ninmonkey/17f6a1f5b28249b6cb2ba8bc746ae4fb>
#>
function FindArchivedUrl {
<#
@ninmonkey
ninmonkey / Hashtable Collisions Differ When Using Literals.md
Last active January 28, 2025 16:26
Colliding Keys Test - Literals don't error and don't lose values

About

I'm experimenting with collisions for the Env var RFC: PowerShell/PowerShell-RFC#384

Cases

Case: fails as expected

$h1 = @{ "path"      = 'foo\bar' }
$h2 = @{ "path`u{0}" = '' }
@ninmonkey
ninmonkey / Dynamic Winget Arguments.pwsh.ps1
Last active January 11, 2025 22:44
Dynamic Winget Arguments using Powershell Pwsh
function InvokeWingetInstall {
<#
.synopsis
example of using native commands with dynamic conditions.
.example
# example commands: Use -TestOnly and -Verbose to see cli arguments, without invoking it.
InvokeWingetInstall -Verbose -Package 'Microsoft.PowerBI' -TestOnly -Silent
VERBOSE: install --id Microsoft.PowerBI /silent
@ninmonkey
ninmonkey / Compare Autoloading Commands.ps1
Last active January 4, 2025 21:48
Test autoloading pwsh modules when using Get-Command and Get-Module
# I'm explicitly unloading the module for each test
Remove-Module ImportExcel -ea 'ignore'
(Get-Module ImportExcel).IterCommands.count
# out: 0
Remove-Module ImportExcel -ea 'ignore'
(Get-Module ImportExcel).count
# out: 0
Remove-Module ImportExcel -ea 'ignore'
@ninmonkey
ninmonkey / Json With Nan Comparison.md
Last active December 6, 2024 16:11
ConvertTo-Json using Nan and Infinity Comparison

About

Compare if NaN fails a round trip in Powershell or when using System.Text.Json

Ran using: pwsh.exe 7.4. context

Tests

@ninmonkey
ninmonkey / DAX using fancy CSS Variables instead of strings.md
Last active January 8, 2025 19:01
Appending strings in DAX templates does not scale well, for the human

SVG's support CSS variables.

CSS variables

Instead of hard coding values in the whole file:

<path fill="#123456" .... >

You can declare a variable named --color-main and resuse it.

@ninmonkey
ninmonkey / Compare Conversions using numeric literals and operators.ps1
Last active November 2, 2024 21:16
Test which conversions occur in pwsh numeric literals
@'
There's a bunch of literals added in 6+
7.1 added
> using a type suffix on a hex literal now returns a signed value of that type
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_numeric_literals?view=powershell-7.5
'@
100 - 93.79 | ty 'i int, i d'
@ninmonkey
ninmonkey / Text to bytes to and HexString, round trip.ps1
Last active November 2, 2024 20:11
Nonsense example. Just showing a round trip.
$originalMessage = "hi 🐒 world"
$enc = [Text.Encoding]::GetEncoding('utf-8')
$bytes = $enc.GetBytes( $originalMessage)
$hexStr = [Convert]::ToHexString( $bytes )
$asBytes = [Convert]::FromHexString( $hexStr )
$roundTrip = $enc.GetString( $AsBytes )
[pscustomobject]@{