Skip to content

Instantly share code, notes, and snippets.

Avatar

Steve Lee SteveL-MSFT

View GitHub Profile
View F7_PSHistory.ps1
$parameters = @{
Key = 'F7'
BriefDescription = 'ShowMatchingHistoryOcgv'
LongDescription = 'Show Matching History using Out-ConsoleGridView'
ScriptBlock = {
param($key, $arg) # The arguments are ignored in this example
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
$history = [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems().CommandLine | Select-Object -Unique
View FizzBuzz.ps1
1..100|%{$_%15?$_%3?$_%5?$_ :'Buzz':'Fizz':'FizzBuzz'|Write-Host}
View 12DaysOfChristmas.ps1
nal w write-host
$d='First','Second','Third','Fourth','Fifth','Sixth','Seventh','Eighth','Ninth','Tenth','Eleventh','Twelfth',"A Partridge in a Pear Tree.`n",'Two Turtle Doves, and','Three French Hens,','Four Calling Birds,','Five Gold Rings,','Six Geese-a-Laying,','Seven Swans-a-Swimming,','Eight Maids-a-Milking,','Nine Ladies Dancing,','Ten Lords-a-Leaping,','Eleven Pipers Piping,','Twelve Drummers Drumming,'
$l="On the % day of Christmas`nMy true love sent to me"
0..11|%{w $l.Replace("%",$d[$_])
$_..0|%{w $d[$_+12]}}
@SteveL-MSFT
SteveL-MSFT / render.ps1
Last active May 1, 2020
Example using System.Commandline.Rendering.dll
View render.ps1
using namespace System.CommandLine.Rendering
add-type -AssemblyName ./System.CommandLine.Rendering.dll
$cs = [ContainerSpan]::new(
[StyleSpan]::UnderlinedOn(),
[ForegroundColorSpan]::Green(),
[BackgroundColorSpan]::Yellow(),
[ContentSpan]::new("hello"),
[StyleSpan]::UnderlinedOff()
@SteveL-MSFT
SteveL-MSFT / graphql.ps1
Last active Apr 1, 2020
GraphQL Query Example
View graphql.ps1
$query = @"
query {
organization(login: "powershell") {
name
url
repository(name: "powershell") {
name
pullRequests(last: 20, states: [OPEN]) {
edges {
node {
View Format-Table for Jupyter.ps1
function Format-Table {
param([parameter(valuefrompipeline=$true)]$inputObject)
begin {
$objects = [System.Collections.Generic.List[PSObject]]::new()
}
process {
$objects += $inputObject
}
View perftest.ps1
$num = 50
$ps = @("powershell.exe", "C:\Program Files\PowerShell\6\pwsh.exe", "C:\Program Files\PowerShell\7-preview\pwsh.exe")
$ps | ForEach-Object {
$thisps = $_
$psv = & $thisps -nop -c '$psv = $psversiontable.gitcommitid; if ($psv -eq $null) { $psv = $psversiontable.psversion.tostring() }; $psv'
[int]$i = 0
1..$num | ForEach-Object {
$i += (Measure-Command {
& $thisps -nop -c exit
}).TotalMilliseconds
View wpf.ps1
add-type -AssemblyName presentationframework
$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window" Title="Initial Window" WindowStartupLocation = "CenterScreen"
Width = "400" Height = "300" ShowInTaskbar = "True">
</Window>
"@
View getargv0.cs
using System;
using System.Runtime.InteropServices;
namespace myps
{
public static class GetProcArgv
{
private const int CTL_KERN = 1;
private const int KERN_ARGMAX = 8;
private const int KERN_PROCARGS2 = 49;
@SteveL-MSFT
SteveL-MSFT / profile.ps1
Last active Oct 20, 2020
PowerShell Prompt
View profile.ps1
#Requires -Version 6
# Version 1.2.4
# check if newer version
$gistUrl = "https://api.github.com/gists/a208d2bd924691bae7ec7904cab0bd8e"
$latestVersionFile = Join-Path -Path ~ -ChildPath ".latest_profile_version"
$versionRegEx = "# Version (?<version>\d+\.\d+\.\d+)"
if (Test-Path $latestVersionFile) {
You can’t perform that action at this time.