Skip to content

Instantly share code, notes, and snippets.

View markwragg's full-sized avatar

Mark Wragg markwragg

View GitHub Profile
@markwragg
markwragg / Get-CoinFlip.ps1
Created November 28, 2017 08:49
PowerShell Function for deciding between two choices. E.g Get-CoinFlip -Heads Coffee -Tails Tea
Function Get-CoinFlip {
Param(
$Heads = 'Heads',
$Tails = 'Tails'
)
Get-Random $Heads,$Tails
}
@markwragg
markwragg / Format-ColourList.ps1
Last active September 4, 2017 08:48
PowerShell function to create a partially coloured version of Format-List to highlight defined keywords in one or more colours.
Function Format-ColourList {
Param(
#The input object to be formatted.
[Parameter(Position=0,ValueFromPipeline)]
$InputObject,
#A hashtable of text to colour and the colour of that text. By default colours 'True' as Green and 'False' as Red.
[Hashtable]$Colour = @{True = 'Green'; False = 'Red'}
)
Begin {
@markwragg
markwragg / Install.ps1
Last active August 30, 2017 07:39
PowerShell script to install a module from a source control system. For use where package management is not an option.
[cmdletbinding()]
Param()
<#
.SYNOPSIS
Installs a module.
#>
Function Install-Module {
[cmdletbinding(SupportsShouldProcess)]
Param (
@markwragg
markwragg / working-days.ps1
Last active September 5, 2017 08:09
Number of working days between two dates
#As a function
Function Get-WorkingDays ($Date) {
(0..((Get-Date $Date) - (Get-Date)).days | % { (Get-Date).AddDays($_) } | Where-Object DayOfWeek -notin 'Saturday','Sunday').count
}
Get-WorkingDays 15/09/2017
#One liner
(0..((Get-Date '15/09/2017') - (Get-Date)).days | % { (Get-Date).AddDays($_) } | ? DayOfWeek -notin 'Saturday','Sunday').count

New User

Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve).

Code Writing

@markwragg
markwragg / Remove-WhiteSpace.ps1
Created June 30, 2017 14:45
PowerShell function to remove spurious whitespace from scripts. Shared by JohnLBevan
function Remove-WhiteSpace {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string]$Path
,
[Parameter()]
[string]$Encoding = 'UTF8' #type FileSystemCmdletProviderEncoding is not public and does not match [System.Text.Encoding
)
process {
@markwragg
markwragg / Write-Log.ps1
Created June 26, 2017 21:05
Simple PowerShell logging function with timestamp, redirects messages to a log file and the verbose stream.
function Write-Log {
Param(
$Message,
$Path = "$env:USERPROFILE\log.txt"
)
function TS {Get-Date -Format 'hh:mm:ss'}
"[$(TS)]$Message" | Tee-Object -FilePath $Path -Append | Write-Verbose
}
@markwragg
markwragg / cardgame.ps1
Last active June 26, 2017 20:05
Card game generator
#Generate the deck
$Suits = 'Hearts','Diamonds','Spades','Clubs'
$Pack = ForEach ($Suit in $Suits) {
2..10 + 'Jack','Queen','King','Ace' | ForEach-Object { "$_ of $Suit" }
}
#Shuffle the deck between 1 and 5 times
1..(Get-Random -Min 1 -Max 5) | ForEach-Object { $Pack = $Pack | Sort-Object {Get-Random} }
#Deal 5 cards to each player and sort them in natural order
@markwragg
markwragg / Calculated-Expression-Examples.ps1
Last active June 5, 2017 14:56
Demo - Calculated Properties
#https://technet.microsoft.com/en-us/library/ff730948.aspx
Get-Process
Get-Process | Select-Object Name, VM, PM -First 10
Get-Process | Select-Object Name, @{name='VirtualMemory (MB)'; expression={$_.VM / 1MB}}, PM -First 10
Get-Process | Select-Object Name, @{name='VirtualMemory (MB)'; expression={ '{0:N2}' -f ($_.VM / 1MB)}}, PM -First 10
@markwragg
markwragg / ConvertDateProperties.ps1
Last active May 19, 2017 11:43
PowerShell Function to recurse all levels of a nested object and convert valid date strings to [datetime] properties
Function Convert-DateProperties {
<#
.SYNOPSIS
Converts string properties that have 'date' in their name to datetime properties, recursively in all levels of an object.
.PARAMETER object
The object to convert.
.PARAMETER name
The string that should be matched in the name property of the object. Default = 'Date'.
.EXAMPLE
$MyObj | Convert-DateProperties