Skip to content

Instantly share code, notes, and snippets.

View powercode's full-sized avatar

Staffan Gustafsson powercode

  • DICE
  • Stockholm, Sweden
  • 13:56 (UTC +02:00)
View GitHub Profile
@powercode
powercode / MatchInfoV5.format.ps1xml
Last active April 18, 2019 01:35
MatchInfoV5.format.ps1xml
<Configuration>
<!-- Add with Update-FormatData -PrependPath -->
<ViewDefinitions>
<View>
<Name>MatchInfo</Name>
<ViewSelectedBy>
<TypeName>Microsoft.PowerShell.Commands.MatchInfo</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
@powercode
powercode / StandardDeviation.ps1
Last active August 1, 2020 03:07
Calculate Standard Deviation is PowerShell
using namespace System.Collections.Generic
using namespace System.Linq
class StandardDeviation{
hidden [decimal] $Average
StandardDeviation([decimal] $average){
$this.Average = $average
}
@powercode
powercode / solutions.ps1
Last active December 20, 2016 15:33
Sample solutions to practice tasks
# practice tasks
# task1: Get all processes that on your machine where the processname starts with 'po'
Get-Process -name po*
#task 2: Get all processes where the handlecount > 1000
Get-Process | Where-Object HandleCount -gt 1000
#task 3: How many files are there in %windir%\System32
Get-ChildItem -File -LiteralPath $env:windir/System32 | Measure-Object
@powercode
powercode / touch.ps1
Created March 17, 2017 19:40
Touch in PowerShell
using namespace System.Management.Automation
function Update-LastWriteTime {
[CmdletBinding(DefaultParameterSetName='Path', SupportsShouldProcess)]
[OutputType([IO.FileInfo])]
[Alias('touch')]
param(
[Parameter(Mandatory, ParameterSetName="Path", Position=0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[string[]] $Path,
[Alias('PSPath')]
#requires -Version 5.0
using namespace System.Management.Automation
using Namespace System.Collections
using Namespace System.Collections.Generic
<#
.SYNOPSIS
Combine items from the pipeline into a string, optionally adding single or double quotes
@powercode
powercode / Ripgrep.ArgumentCompleters.ps1
Last active April 10, 2017 18:40
Argument completer for ripgrep (rg)
using namespace System.Collections.Generic
using namespace System.Management.Automation
class RGCompleter {
static [bool] ShouldAdd([string] $completionText, [string] $wordToComplete, [string[]] $previousCommand, [string[]] $multiOpt) {
if ($completionText.StartsWith($wordToComplete, [StringComparison]::OrdinalIgnoreCase)){
if ($completionText -in $previousCommand -and $completionText -notin $multiOpt) {
return $false
}
return $true
@powercode
powercode / Ripgrep.ArgumentCompleters.gen.ps1
Last active April 10, 2017 20:15
Generation of ArgumentCompleter for ripgrep
. C:\Users\Staffan\Documents\WindowsPowerShell\JoinItem.ps1
class OptionHelp {
[string] $Short
[string] $Long
[string] $Arg
[string] $Help
}
@powercode
powercode / PEHeader.ps1
Last active July 10, 2018 14:14
PE Header reader for PowreShell
using namespace System.Management.Automation
using namespace System.IO
using namespace System.Collections.Generic
class PEHeaders {
[String] $Path
[CoffHeader] $Coffheader
[PEHeader] $PEHeader
[SectionHeader[]] $SectionHeaders
[DebugDirectoryEntry[]] $DebugDirectoryEntires
@powercode
powercode / ConvertFromSid.ps1
Created May 16, 2017 20:26
Convert sid to nt account
using namespace System.Security.Principal
function ConvertFrom-SID {
[CmdletBinding()]
[OutputType([NTAccount])]
param([SecurityIdentifier[]] $SID)
$SID.Foreach{$_.Translate([NTAccount])}
}
@powercode
powercode / ResetAnsi.ps1
Created June 27, 2017 12:30
Reset-Ansi terminal
Add-Type -MemberDefinition @"
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, int mode);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern IntPtr GetStdHandle(int handle);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool GetConsoleMode(IntPtr handle, out int mode);