Skip to content

Instantly share code, notes, and snippets.

View powercode's full-sized avatar

Staffan Gustafsson powercode

  • DICE
  • Stockholm, Sweden
  • 22:32 (UTC +02:00)
View GitHub Profile
@powercode
powercode / ImportCsvWithType.ps1
Last active August 21, 2023 14:58
Automatic generation of a class to store data from import-csv
function New-TypeDefinitionFromCsv {
param(
[Parameter(Mandatory)]
[string] $Path,
[ValidatePattern("\w[\w\d_-]*", ErrorMessage = "The typename must be a valid name of an Identifier.")]
[string] $NameOfGeneratedType,
[ArgumentCompletions('";"', '"`t"', '","')]
[string] $Delimiter = ',',
[Type[]] $ColumnType,
[switch] $CSharp
@powercode
powercode / Send-PSNotification.ps1
Last active January 28, 2019 12:53
Send-PSNotification
<#
.SYNOPSIS
Sends a notification on Linux.
.DESCRIPTION
Sends a notification on Linux. Under the hood, this uses notify-send(1)
.PARAMETER Body
The body or main content of the notification.
@powercode
powercode / Send-PSNotification.ps1
Created January 25, 2019 15:40
Send-PSNotification
<#
.SYNOPSIS
Sends a notification on Linux.
.DESCRIPTION
Sends a notification on Linux. Under the hood, this uses notify-send(1)
.PARAMETER Body
The body or main content of the notification.
@powercode
powercode / Set-PowerPointLanguage.ps1
Last active April 9, 2018 06:23
Set-PowerPointLanguage
<#
.SYNOPSIS
Change language settings for a PowerPoint presentation
.EXAMPLE
PS> Set-PowerPointLanguage -Path MyPres.pptx
Writes a copy of MyPres.pptx to MyPres.en-us.pptx with the language set to 'en-US' - English (the default)
.EXAMPLE
PS> Set-PowerPointLanguage -Path MyPres.pptx -Language de-DE
using namespace System.Management.Automation
using namespace System.Collections.Generic
class ArgumentCompleterBase : IArgumentCompleter {
hidden [List[CompletionResult]] $Results = [List[CompletionResult]]::new()
[string] $WordToComplete
[Language.CommandAst] $commandAst
# override in child class
[void] AddCompletionsFor([string] $commandName, [string] $parameterName, [Collections.IDictionary] $fakeBoundParameters) {}
@powercode
powercode / ArgumentCompleterBase.cs
Last active February 25, 2018 14:22
CompleterBase : base class for PowerShell argument completers
/// <summary>
/// Base class for writing custom Argument Completers
/// </summary>
/// <para>Derived classes should override <see cref="AddCompletionsFor(string,string,IDictionary)"/> </para>
public abstract class ArgumentCompleterBase : IArgumentCompleter
{
private List<CompletionResult> _results;
/// <summary>
@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);
@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 / 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 / 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
}