Dyota's blog

PowerShell Cookbook

This is an assorted collection of PowerShell snippets that I've used in the past and might need to use again.

Text functions

Get-Clipboard | ConvertFrom-Csv -Delimiter "`t" | ConvertTo-Html -Fragment | Set-Clipboard
function compressText ($path) { return ((cat $path) -join "`n" -replace "\s+", " ").Trim() }
function checkTextEqual ($target, $reference) {
     return (compressText $target) -eq (compressText $reference)
}
function regexMatchAll ($regex, $text) {
    return ($text | Select-String  $regex -AllMatches).Matches.Value
}

Object functions

Check if object has property

[bool]($_.PSobject.Properties.name -match "propertyname"

Image functions

Convert-ImageToBase64

This converts a local picture into a base 64 string for the purposes of embedding in a <img> tag

function Convert-ImageToBase4 ([string] $filepath) {
    $imageBase64 = [Convert]::ToBase64String((Get-Content "$root\images\one-tile.png" -Encoding byte))
    return "data:image/png;base64,$imageBase64"
}