Skip to content

Instantly share code, notes, and snippets.

View markwragg's full-sized avatar

Mark Wragg markwragg

View GitHub Profile
@markwragg
markwragg / Format-HashTable.ps1
Last active August 2, 2023 11:16
Function to convert an object into HashTable PowerShell code, with name values optionally anonymized. This was written to quickly create the contents of Pester Mocks, by taking real output and converting it to Hashtables or PsCustomObject output for declaring within the Mock in Pester. The function can anonymize the name strings in the objects a…
function Format-HashTable {
[CmdletBinding()]
param(
[parameter(, ValueFromPipeline)]
$InputObject,
[switch]
$AnonymizeNames,
[switch]
@markwragg
markwragg / Get-AppInsightsUsage.ps1
Created April 25, 2020 12:42
A PowerShell function for querying one or more AppInsights accounts (or all AppInsights accounts under a subscription once logged in) to get the daily cap setting and previous 24 hours of usage
Function Get-AppInsightsUsage {
Param(
# Optional: One or more AppInsights resource names
[string[]]
$Name
)
$AppInsights = Get-AzApplicationInsights
If ($Name) { $AppInsights = $AppInsights | Where-Object { $_.Name -in $Name } }
@markwragg
markwragg / profile.ps1
Created March 5, 2020 09:30
PowerShell Profile snippet to save/return to last location on exit
$LastLocationFile = Join-Path $env:USERPROFILE 'lastlocation.txt'
If (Test-Path $LastLocationFile) {
Import-Clixml $LastLocationFile | Set-Location
}
Else {
Set-Location $env:USERPROFILE
}
Register-EngineEvent PowerShell.Exiting -Action {

How to get @DevBlackOps Terminal-Icons module working in PowerShell on Windows

Note: since version 0.1.1 of the module this now works in Windows PowerShell or PowerShell Core.

  1. Download and install this version of Literation Mono Nerd Font which has been specifically fixed to be recognised as monospace on Windows:

https://github.com/haasosaurus/nerd-fonts/blob/regen-mono-font-fix/patched-fonts/LiberationMono/complete/Literation%20Mono%20Nerd%20Font%20Complete%20Mono%20Windows%20Compatible.ttf

(see this issue for more info: ryanoasis/nerd-fonts#269)

@markwragg
markwragg / Write-Human.ps1
Last active October 3, 2023 14:33
A PowerShell function to output text to screen as if typed by a human. I don't know why I wrote this.
Function Write-Human {
<#
.SYNOPSIS
Use to output text as if typed by a human.
.SYNOPSIS
This script takes one or more strings and prints them to the screen with a slightly randomised delay between each character to emulate a human typing.
.EXAMPLE
Get-Content .\mytestfile.txt | Write-Human
#>
[cmdletbinding()]
@markwragg
markwragg / Get-Subnet.ps1
Last active April 5, 2024 14:32
PowerShell cmdlet to return the IP details and range of a network and subnet mask
function Get-Subnet {
param (
[parameter(ValueFromPipeline)]
[String]
$IP,
[ValidateRange(0, 32)]
[int]
$MaskBits,
@markwragg
markwragg / get-awsami.ps1
Last active October 29, 2018 08:30
PowerShell / AWS CLI command to retrieve list of Amazon AMIs matching a partial name string
Function Get-AWSAMI {
[cmdletbinding()]
Param(
$Name = 'Windows_Server-2016-English'
)
$Images = aws ec2 describe-images --filters Name="name",Values="$Name" --region us-west-2 --owners amazon
($Images | ConvertFrom-Json).Images
}
@markwragg
markwragg / settings.json
Created October 23, 2018 10:15
VSCode setting PowerShell as default terminal on MacOS
{
"files.defaultLanguage": "powershell",
"powershell.powerShellExePath": "/usr/local/bin/pwsh",
"terminal.integrated.shell.osx": "/usr/local/bin/pwsh",
"terminal.integrated.shellArgs.osx": [],
}
@markwragg
markwragg / find-cmdlets.ps1
Last active October 18, 2018 11:02
Find all cmdlets being used in a specified script by using Abstract Syntax Trees
$AST = [System.Management.Automation.Language.Parser]::ParseFile(
(Resolve-Path './Some-File.ps1'),
[ref]$null,
[ref]$Null
)
$CommandElements = ($AST.FindAll( {$args[0] -is [System.Management.Automation.Language.CommandAst]}, $true)).CommandElements
($CommandElements | Where-Object {$_.StringConstantType -eq 'BareWord'}).Value | Sort-Object -Unique
@markwragg
markwragg / Open-GitUrl.ps1
Last active February 7, 2023 13:26
A PowerShell function to open the remote URL in the default browser for the git repo folder you are currently in at the command-line.
function Open-GitUrl {
$RemoteURL = git config remote.origin.url
if ($RemoteURL) {
if ($RemoteURL -match '\.git$') { $RemoteURL = $RemoteURL -replace '\.git$','/' }
if ($IsMacOS) {
open $RemoteURL