Skip to content

Instantly share code, notes, and snippets.

@sean-m
sean-m / dir_compressor.ps1
Last active March 7, 2017 01:21
Will take a source and destination directory then spawn a 7za.exe job for each directory. These are enqueued and executed in PowerShell jobs with a throttle to ensure only a limited number of compression jobs happen at any given time.
$queue = New-Object System.Collections.Queue
$wait_state = 2
$job_count = 0
$completed_request = @{}
$job_dispached = @{}
$throttle = 3
$source_dir = "D:\"
$dest_dir = "E:\"
@sean-m
sean-m / Generate-Password.ps1
Last active March 28, 2017 20:35
Creates a random password with some options.
<#
Make 20 character password with letters and numbers
Generate-Password 20 -NoSpecial
Make default length password (12) with full typable ascii set
Generate-Password
Create 20 character password using alias for shorthand
gpw 20
@sean-m
sean-m / Histogram.ps1
Last active March 6, 2019 07:23
Generate simple histogram in PowerShell. Displays frequency that an item occurs in a list.
function Histogram {
[CmdletBinding()]
[Alias("hgram")]
param (
[Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[object[]]$Items,
[Parameter(Position=1)]
[string]$Property,
[ValidateRange(2,[int]::MaxValue)]
[int]$Scale = 40,
@sean-m
sean-m / Random-Thing.ps1
Last active March 27, 2017 19:26
Gives you a random adjective noun/verb phrase. Inspired by the crazy naming scheme used by our intelligence community.
function Random-Thing {
[Alias("rt")]
[OutputType([string])]
param ([switch]$FullPhrase, [switch]$XKCD)
begin {
$adjectives = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("YWJhbmRvbmVkIGFibGUgYWJzb2x1dGUgYWRvcmFibGUgYWR2ZW50dXJvdXMgYWNhZGVtaWMgYWNjZXB0YWJsZSBhY2NsYWltZWQgYWNjb21wbGlzaGVkIGFjY3VyYXRlIGFjaGluZyBhY2lkaWMgYWNyb2JhdGljIGFjdGl2ZSBhY3R1YWwgYWRlcHQgYWRtaXJhYmxlIGFkbWlyZWQgYWRvbGVzY2VudCBhZG9yYWJsZSBhZG9yZWQgYWR2YW5jZWQgYWZyYWlkIGFmZmVjdGlvbmF0ZSBhZ2VkIGFnZ3JhdmF0aW5nIGFnZ3Jlc3NpdmUgYWdpbGUgYWdpdGF0ZWQgYWdvbml6aW5nIGFncmVlYWJsZSBhamFyIGFsYXJtZWQgYWxhcm1pbmcgYWxlcnQgYWxpZW5hdGVkIGFsaXZlIGFsbCBhbHRydWlzdGljIGFtYXppbmcgYW1iaXRpb3VzIGFtcGxlIGFtdXNlZCBhbXVzaW5nIGFuY2hvcmVkIGFuY2llbnQgYW5nZWxpYyBhbmdyeSBhbmd1aXNoZWQgYW5pbWF0ZWQgYW5udWFsIGFub3RoZXIgYW50aXF1ZSBhbnhpb3VzIGFueSBhcHByZWhlbnNpdmUgYXBwcm9wcmlhdGUgYXB0IGFyY3RpYyBhcmlkIGFyb21hdGljIGFydGlzdGljIGFzaGFtZWQgYXNzdXJlZCBhc3RvbmlzaGluZyBhdGhsZXRpYyBhdHRhY2hlZCBhdHRlbnRpdmUgYXR0cmFjdGl2ZSBhdXN0ZXJlIGF1dGhlbnRpYy
@sean-m
sean-m / Get-PerformanceInfo.ps1
Created April 7, 2017 22:57
PowerShell wrapper fro the GetPerformanceInfo system call
<# PowerShell wrapper fro the GetPerformanceInfo system call #>
function Get-PerformanceInfo {
## Liberally taken from Pinvoke.net, thank you whoever contributed that
## http://www.pinvoke.net/default.aspx/psapi/GetPerformanceInfo.html
Add-Type -Language CSharp -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public static class PerformanceInfo
{
[DllImport("psapi.dll", SetLastError = true)]
Get-Service -Name TrustedInstaller | Stop-Service -Verbose
ii "C:\Windows\Logs\CBS\"
Start-Sleep -Seconds 5
Get-ChildItem "C:\Windows\Logs\CBS\*" -Recurse -Force -ErrorAction SilentlyContinue |
Where-Object { $_.Name -ne 'CBS.Log' } |
Remove-Item -force -Verbose -recurse -ErrorAction Continue
@echo off
echo.
echo "Stop services"
::pause
sc stop msiserver
sc stop TrustedInstaller
echo.
echo "Disable services"
@sean-m
sean-m / GenAsmLoader.ps1
Last active May 31, 2018 20:05
Helper function for wrapping DLL assembly in PowerShell so it can more easily be placed in version control and loaded within a script.
function GenAsmLoader {
<#
.Synopsis
Helper function for wrapping DLL assembly in PowerShell so it
can more easily be placed in version control and loaded within a script.
.DESCRIPTION
The ability to ship binary modules inside of a script file can be nice
if working in an environment that does not allow external dependencies.
This is a way around that by allowing you to bundle .Net DLL libraries inside
PowerShell code and providing a function that may be called to load the
function Get-FileSize {
<#
.Synopsis
Gets file info, recursively if desired.
.DESCRIPTION
Enumerates files in a directory getting both the regular
and compressed size. The enumeration happens in a pipeline
function so it's memory efficient. The input parameter $Path
is checked for type so if you pipe the output of Get-ChildItem
@sean-m
sean-m / Ternary.ps1
Last active August 25, 2017 19:08
My take on a ternary operation for PowerShell, I use it for my stuff and make no promises. Enjoy!
<#
.Synopsis
Ternary operation for PowerShell with terse
parameter aliases. Meant to be used from pipeline.
.Example
gci -Recurse | >> {$_.PSIsContainer} "Directory" "File"
.Example
1..10 | >> {($_ % 2) -eq 0} "Even" "Odd"
.Example
# Pass through numbers greater than 0 or print 0