Skip to content

Instantly share code, notes, and snippets.

View jhoneill's full-sized avatar

James O'Neill jhoneill

View GitHub Profile
Set-PSReadlineOption -PredictionSource History
Set-PSReadlineOption -BellStyle None -EditMode Windows -WordDelimiters "`~!£#%^&*()=+[{]}\|;:'`",.<>/?"
Set-PSReadLineOption -Colors @{'Parameter'= "$([char]27)[97m"; 'operator'= "$([char]27)[97m"} #defaults are too dark use $([char]27) because `e doesn't work on PS 5.
Set-PSReadLineKeyHandler -Key Ctrl+RightArrow -ScriptBlock {
param($key, $arg)
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
if ($cursor -lt $line.Length) {
[Microsoft.PowerShell.PSConsoleReadLine]::ForwardWord($key, $arg)
#requires -module psreadline
using namespace System.Collections
using namespace System.Collections.Generic
using namespace System.Management.Automation
using namespace System.Management.Automation.Language
#create a hash table of VT codes
# Reverse, underline and reset; the console colors as Name, NameBackground; Markdown theme colors if present, PsReadline theme colors, and out-stream colors
if (-not $host.UI.SupportsVirtualTerminal) {$Script:VTCodes = @{}}
else {
function one {[cmdletbinding()]param()
begin {Write-Host "one begins"} end {Write-host "one ends"}
process {1,2,3}
}
function two {param ([parameter(ValueFromPipeline)]$p)
begin {Write-Host "two begins"} end {Write-host "two ends"}
process {
if ($p -eq 2) {$p / 0 } # cause a division by zero error
$p
}
function binaryout {
<#
.SYNOPSIS
Workaround for PowerShell processing the output of all external programs as strings
.DESCRIPTION
PowerShell treats any output from an external program as string which should be
split whenever it sees LF or CR LF. As a workround this calls the program with
Start-Process and redirects standard output to a file - on completion the file
is read and sent as a bytestream to the next process.
function Get-Error {
[cmdletbinding(DefaultParameterSetName='Newest')]
param (
[Parameter(Position = 0, ValueFromPipeline=$true, ParameterSetName= "Error")]
[ValidateNotNullOrEmpty()]
$InputObject,
[Parameter(ParameterSetName = "Newest", ValueFromPipelineByPropertyName = $true)]
[Alias('Last')]
[ValidateRange(1, [int]::MaxValue)]
<Configuration><ViewDefinitions>
<View><Name>MatchInfo</Name>
<ViewSelectedBy><TypeName>Microsoft.PowerShell.Commands.MatchInfo</TypeName></ViewSelectedBy>
<CustomControl><CustomEntries><CustomEntry><CustomItem><ExpressionBinding><ScriptBlock>
#defaults are equivalent to $MatchInfoPreference =@{$Prefix='> '; $MatchVTSeq=$PSStyle.Reverse; $PathVTSeq=''; $NumberVTSeq=''; $ContextVTSeq=''}
$ResetVTSeq = $PSStyle.Reset
$Prefix = '> '
if (-not $MatchInfoPreference) {$MatchVTSeq = $PSStyle.Reverse }
else {
$MatchVTSeq = $MatchInfoPreference.MatchVTSeq
<?xml version="1.0" encoding="utf-8" ?>
<!-- *******************************************************************
Copyright (c) James O'Neill 2022 - Shared under the MIT License https://mit-license.org/
Allows [consoleColor] to be used like this
ps > [ConsoleColor]::Red.Foreground("This is red")
ps > [ConsoleColor]::DarkYellow.Background([ConsoleColor]::red.Foreground("This is red on darkyellow"))
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<SelectionSets><SelectionSet>
<Name>FileSystemTypes</Name>
<Types>
<TypeName>System.IO.DirectoryInfo</TypeName>
<TypeName>System.IO.FileInfo</TypeName>
</Types>
</SelectionSet></SelectionSets>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
####
#### Proxy function to restore Writing the URI to verbose when calling Invoke-RestMethod
#### When calling out of a module -Verbose isn't inherited so the function fiddles verbosepreference as a parameter.
Function Invoke-RestMethod {
[CmdletBinding(DefaultParameterSetName='StandardMethod', HelpUri='https://go.microsoft.com/fwlink/?LinkID=2096706')]
<#
.ForwardHelpTargetName Microsoft.PowerShell.Utility\Invoke-RestMethod
.ForwardHelpCategory Cmdlet
#>
param(