Skip to content

Instantly share code, notes, and snippets.

View gravejester's full-sized avatar

Øyvind Kallstad gravejester

View GitHub Profile
@gravejester
gravejester / Measure-ScriptBlock.ps1
Last active July 20, 2022 18:48
Measure-ScriptBlock
function Measure-ScriptBlock {
<#
.SYNOPSIS
Measure time, memory consumption and CPU time for a block of code to execute.
.DESCRIPTION
Measure time, memory consumption and CPU time for a block of code to execute.
This function is using Measure-Command internally, but is also trying to record how much memory and CPU time the code uses during execution.
.EXAMPLE
@gravejester
gravejester / Get-TrimmedMean.ps1
Last active August 29, 2015 14:08
Get-TrimmedMean
function Get-TrimmedMean {
<#
.SYNOPSIS
Function to calculate the trimmed mean of a set of numbers.
.DESCRIPTION
Function to calculate the trimmed mean of a set of numbers.
The default trim percent is 25, which when used will calculate the Interquartile Mean of the number set.
Use the TrimPercent parameter to set the trim percent as needed.
.EXAMPLE
[double[]]$dataSet = 8, 3, 7, 1, 3, 9
@gravejester
gravejester / Get-Mockaroo.ps1
Last active August 29, 2015 14:08
Get-Mockaroo
# the EnumUtils code was nicked from Wayne Hartman (http://blog.waynehartman.com/articles/84.aspx)
Add-Type -TypeDefinition @"
using System;
using System.Reflection;
using System.ComponentModel;
public enum MockarooType
{
Boolean,
City,
function New-FormatTableItem {
<#
.SYNOPSIS
Create new format Table Item
.DESCRIPTION
Create new format Table Item for use with Add-FormatTable
.EXAMPLE
Assuming you have a custom object with a type name of 'Custom.UserType':
@(
function Resolve-PathEx {
<#
.SYNOPSIS
Resolve-Path extended to also work with files that don't exist.
.DESCRIPTION
You can use Resolve-PathEx when you want to handle both filenames and paths in a single parameter in your functions.
The function returns an object, and includes the resolved path as well as a boolean indicating whether the file
exists or not. Wildcards are supported for both path and filename.
.EXAMPLE
Resolve-Path *.ps1
function Invoke-Touch {
<#
.SYNOPSIS
PowerShell inplementation of the Unix/Linux utility called touch.
.DESCRIPTION
Touch let's you update the access date and / or modification date of a file. If the file don't exist, an empty file will be created
unless you use the DoNotCreateNewFile parameter. This implementation have the original parameter names added as
aliases, so if you are familiar with the original touch utility it should be easy to use this one.
.EXAMPLE
Invoke-Touch newfile
function New-Log {
<#
.SYNOPSIS
Create a new log.
.DESCRIPTION
The New-Log function is used to create a new log file or Windows Event log. A log object is also created
and either saved in the global PSLOG variable (default) or sent to the pipeline. The latter is useful if
you need to write to different log files in the same script/function.
.EXAMPLE
New-Log '.\myScript.log'
# Basic Script Template for Runspace Jobs against an array of computers
# This is just an example to show the basic layout of a script using the Runspace functions
# If you are creating a script to be run in production, please add logging and error handling
# Also consider writing it to support parameters for input instead of hardcoding it like in this example
#region verify that runspace functions are present
$missingFunctions = $false
$functions = (
'New-RunspacePool',
'New-RunspaceJob',
@gravejester
gravejester / Microsoft.PowerShell_profile.ps1
Last active August 29, 2015 14:10
My PowerShell Profile
# PowerShell Profile - Øyvind Kallstad
# Last updated: 27.11.2014
# Get the original prompt
$originalPrompt = (Get-Item function:prompt).ScriptBlock
# define our new custom prompt
$customPrompt = {
$currentLocaction = $executionContext.SessionState.Path.CurrentLocation.ToString()
$host.UI.RawUI.WindowTitle = $currentLocaction.Replace('Microsoft.PowerShell.Core\','')
function Search-Spotify {
<#
.SYNOPSIS
Search Spotify.
.DESCRIPTION
This function uses the public web API of Spotify to let you query their database for information about
artists, albums, tracks and popular playlists.
.EXAMPLE
Search-Spotify 'Madonna'
Will search for 'Madonna' among all artists in the Spotify database.