Skip to content

Instantly share code, notes, and snippets.

View markwragg's full-sized avatar

Mark Wragg markwragg

View GitHub Profile
@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
@markwragg
markwragg / ClassDefinitionFromObject.ps1
Created May 17, 2017 14:08
PowerShell function to create a Class Definition from an existing object. Forked from Brian Scholer (https://github.com/briantist).
function New-ClassDefinitionFromObject {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[object]$InputObject,
[Parameter()]
[String]$Name = 'Root',
[SupportsWildcards()]
@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-LocalTime.ps1
Last active May 30, 2017 08:40
PowerShell function that uses Google APIs to get the current local time for any valid address string. Requires a Google API key.
Function Get-LocalTime {
[cmdletbinding()]
Param(
[Parameter(Mandatory=$true)]
$Address,
$Time = (Get-Date),
#Get an API key from here: https://developers.google.com/apis-explorer/
$APIKey = (Import-Clixml "$env:USERPROFILE\$ENV:USERNAME-GoogleApi.xml"),