Skip to content

Instantly share code, notes, and snippets.

View sassdawe's full-sized avatar
💭
Playing PowerShell

David Sass sassdawe

💭
Playing PowerShell
View GitHub Profile
@sassdawe
sassdawe / ThrowStdOutErrors.ps1
Created November 15, 2022 10:22 — forked from JustinGrote/ThrowStdOutErrors.ps1
Catch only specific errors coming from native commands
filter ThrowStdOutErrors($messageFilter,[Parameter(ValueFromPipeline)]$obj) {
if ($obj -is [Management.Automation.ErrorRecord]) {
if ($obj -match $messageFilter) {
throw $obj
} else {
Write-Error $obj
return
}
}
$obj
@sassdawe
sassdawe / PresentLight.json
Last active April 15, 2025 12:20
A light theme for Windows Terminal designed for the big screen!
{
"background": "#F9F9F9",
"black": "#AB3D2C",
"blue": "#275FE4",
"brightBlack": "#C21458",
"brightBlue": "#0099E1",
"brightCyan": "#7B86BB",
"brightGreen": "#3D942E",
"brightPurple": "#CE33C0",
"brightRed": "#FF0308",
@sassdawe
sassdawe / function-mandatoryUserBoolParam.ps1
Created October 14, 2023 09:30
Mandatory user provided parameter in PowerShell
function mandatoryUserBoolParam {
param(
[Parameter(Mandatory=$true)]
[ValidateSet("true","false","1","0","yes","no","y","n")]
[string]$param
)
$boolParam = $false
switch ($param.ToLower()) {
"true" { $boolParam = $true }
@sassdawe
sassdawe / OrderedDictionary.ps1
Last active May 10, 2024 05:07
System.Collections.Specialized.OrderedDictionary
# option 1
using namespace System.Collections.Specialized
$ordered = new-object OrderedDictionary
# option 2
$ordered = new-object System.Collections.Specialized.OrderedDictionary
# members
$ordered | get-member
@sassdawe
sassdawe / HashSet.ps1
Created May 8, 2024 12:45
HashSet is a hash-based collection that allows only distinct elements
<#
The basics: A HashSet is a collection that holds unique elements in no particular order (O(1) complexity
for adding, searching or removing). The HashSet<T> is a generic class in the System.Collections.Generic
namespace, ideal for managing large data sets and performing set operations.
Core aspects: The dotnet HashSet is a hash-based collection that allows only distinct elements.
It supports various operations such as Union, Intersection, Difference, and more.
More: https://www.bytehide.com/blog/hashset-csharp
#>
@sassdawe
sassdawe / ternary.ps1
Last active April 13, 2025 19:14
Ternary operator for Windows PowerShell v2 and beyond, maybe even for v1
<##################################################################################
#
# Script name: ternary.ps1
# source http://blogs.technet.com/b/heyscriptingguy/archive/2009/06/15/hey-scripting-guy-event-2-solutions-from-expert-commentators-beginner-and-advanced-the-long-jump.aspx
#
##################################################################################>
set-alias ?: Invoke-Ternary -Option AllScope -Description "PSCX filter alias"
filter Invoke-Ternary ([scriptblock]$decider, [scriptblock]$ifTrue, [scriptblock]$ifFalse) {
if (&$decider) {
@sassdawe
sassdawe / xpat-edit-pwsh.ps1
Created May 21, 2025 07:59
get a terminal file editor for every system where you use PowerShell
install-Module psedit
set-alias edit show-pseditor