Skip to content

Instantly share code, notes, and snippets.

View markwragg's full-sized avatar

Mark Wragg markwragg

View GitHub Profile

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 / 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 / Sort-Natural.ps1
Last active February 5, 2024 12:16
PowerShell natural sort. A regex way to sort files that have a number in them correctly, e.g rather than img1, img10, img11, img2, -> img1, img2, img10, img11
# http://stackoverflow.com/a/5429048/2796058
$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
Get-ChildItem | Sort-Object $ToNatural
@markwragg
markwragg / Get-InstalledHotfix.ps1
Last active October 21, 2023 08:24
Powershell script to check whether a hotfix is installed on multiple servers.
[CmdletBinding()]
Param(
$Computers = (Import-csv ".\servers.csv"), #Must include "adaccountname" column
$Patch = "KB2468871"
)
$i = 1
ForEach ($Server in $Computers) {
Write-Progress -Activity "Checking $Server for hotfix $Patch" -Status "$i of $($Computers.Count)" -PercentComplete (($i / $Computers.Count)*100)
@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 / 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 / Compare-DFStoFolders.ps1
Last active July 3, 2023 14:57
Powershell script to get a list of DFS folder targets for all DFS folders under a defined path and test if those paths are valid from the location running the script.
$Servers = @("SERVER01","SERVER02","SERVER03")
$FolderPaths = $Servers | foreach {
Get-ChildItem "\\$_\DFSShare$"
} | Sort Path
$FolderPaths | Export-Csv "FolderPaths-$(Get-Date -format yyyy-MM-dd).csv" -NoTypeInformation
$TestPaths = (($FolderPaths).FullName | Sort-Object).Trimend('\')
$DFSPaths = ((Import-CSV "DFS-$(Get-Date -format yyyy-MM-dd).csv").TargetPath | Where-Object {($_ -ilike "*SERVER*") | Sort-Object).Trimend('\')
@markwragg
markwragg / mactweaks.md
Last active April 25, 2023 18:10
Mac tweaks

Changes to my Mac

Turn off "press and hold" for foreign letters, allowing you to then press and hold for repeat letters. Open Terminal and run:

defaults write NSGlobalDomain ApplePressAndHoldEnabled -bool false

Then you need to close/reopen any app before it takes effect.

Make the dock appear faster when using auto hide:

@markwragg
markwragg / Test-SelectStringExamples.ps1
Last active March 30, 2023 20:17
Powershell uses of Select-String to filter a log file and example Regular Expressions for identifying IP addresses and IP spaces
#This regex matches anything that's like an IP address (even invalid ones)
$regexIPAddress = '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
#This regex matches anything like an IP address that starts 10, 172 or 192
$regexIPSpace = '(10|172|192).\d{1,3}\.\d{1,3}\.\d{1,3}\b'
#Returns the first IP address in each line of the log file/s then sorts and removes duplicates.
Select-String -Path *.log -Pattern $regexIPAddress | ForEach-Object { $_.Matches } | % { $_.Value } | Sort-Object -Unique | Out-File 'UniqueIPs.txt'
#Returns from a selection of Log files any lines which match a certain string pattern
@markwragg
markwragg / ModuleBuild.ps1
Last active March 26, 2023 03:13 — forked from RamblingCookieMonster/zModuleBuild.ps1
A PowerShell script for creating the default scaffolding and files for a new PowerShell module.
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[string]$ModuleName,
[string]$Path = "C:\Users\$env:UserName\Documents\Code\$ModuleName",
[string]$Author = 'Mark Wragg',
[string]$Description = '',
[version]$PSVersion = '3.0'
)