Skip to content

Instantly share code, notes, and snippets.

View scriptingstudio's full-sized avatar
👍
Awake and ready

Matthew Gray scriptingstudio

👍
Awake and ready
  • Interstellar Systems
  • Hiranyaloka
View GitHub Profile
@scriptingstudio
scriptingstudio / testadmin.cmd
Created May 22, 2025 07:16
Culture invariant admin privileges test in cmd
powershell.exe -executionpolicy bypass -noninteractive -nologo -noprofile -command "&{ If (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {write-warning 'You must run this script as an Administrator!'; cmd.exe /c pause; exit 1000} }"
if ERRORLEVEL 1000 goto :EOF
@scriptingstudio
scriptingstudio / testleap.ps1
Last active May 18, 2025 09:11
Test leap year in one line
# But [datetime]::IsLeapYear is faster anyway
function Test-IsLeap ([uint32]$year) {
(($year * 1073750999) -band 3221352463) -le 126976
}
@scriptingstudio
scriptingstudio / xml2string.ps1
Created May 16, 2025 03:00
XML to string converter
function Convert-XmlToString {
param (
[Parameter(Mandatory,ValueFromPipeline)]
$xml
)
begin {
$sw = [System.IO.StringWriter]::new()
$xmlSettings = [System.Xml.XmlWriterSettings]::new()
$xmlSettings.ConformanceLevel = [System.Xml.ConformanceLevel]::Fragment
$xmlSettings.Indent = $true
@scriptingstudio
scriptingstudio / await.ps1
Last active May 2, 2025 08:11
Simple await function to wait for an async task to complete within timeout and return the result if any
function Wait-Task {
param (
[Parameter(Mandatory, ValueFromPipeline)]
[System.Threading.Tasks.Task] $Task,
[alias('wait')][int] $TimeOutMs = 1000
)
begin {if ($TimeOutMs -lt 1) {$TimeOutMs = -1}}
process {
if ($task.AsyncWaitHandle.WaitOne($timeoutMs)) {
$task.GetAwaiter().GetResult()
@scriptingstudio
scriptingstudio / Get-HtmlContent.ps1
Last active May 3, 2025 12:23
Simple Webclient wrapper
function Get-HtmlContent {
[CmdletBinding()]
[alias('ghc')]
param (
[Parameter(Mandatory,ValueFromPipeLine)]
[Uri[]]$Uri,
[alias('ua')][string]$UserAgent,
[System.Text.Encoding]$Encoding,
[alias('wait')][int] $TimeOutMs = -1
)
@scriptingstudio
scriptingstudio / slicestring.ps1
Last active April 6, 2025 10:30
Regex at work. Strange way to slice strings
function Slice-String ([string]$string, [int]$length, [int[]]$part) {
if ($length -gt 1) {
if ($part.count) {
($string -split "(?<=.)(?=(?:.{$length})+$)")[$part]
} else {
$string -split "(?<=.)(?=(?:.{$length})+$)"
}
} else {$string}
}
@scriptingstudio
scriptingstudio / InvokeTimedCommand.ps1
Last active March 29, 2025 10:35
Yet another way to run timed Powershell commands and scripts
function Invoke-PSCommand {
[CmdletBinding(DefaultParameterSetName='scriptblock')]
[alias('ipsc')]
param (
[Parameter(Mandatory,Position=0,ParameterSetName='scriptblock')]
[ValidateNotNullOrEmpty()]
[alias('sb')][ScriptBlock] $ScriptBlock,
[Parameter(Mandatory,Position=0,ParameterSetName='file')]
[ValidateNotNullOrEmpty()]
[alias('fn')][string] $FileName,
@scriptingstudio
scriptingstudio / get-startitem.ps1
Last active March 22, 2025 14:52
List Start Menu Items in One Line
"C:\ProgramData\Microsoft\Windows\Start Menu\*.lnk",
"$($ENV:APPDATA)\Microsoft\Windows\Start Menu\Programs\*.lnk" |
Get-ChildItem -Recurse | ForEach-Object {
[PSCustomObject]@{
Name = $_.baseName
Shortcut = $_.Fullname
}
} | Sort-Object Name
@scriptingstudio
scriptingstudio / ConvertEventLogRecord.ps1
Last active February 26, 2025 05:21
Yet Another Windows Event Log Record Expander
function Convert-EventLogRecord {
[cmdletbinding()]
[alias('clr','Format-EventLogRecord')]
param (
[Parameter(Position=0,Mandatory,ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[alias('logrecord','events')]
[System.Diagnostics.Eventing.Reader.EventLogRecord[]]$InputObject
)
@scriptingstudio
scriptingstudio / Use-Culture.ps1
Last active March 16, 2025 08:45
Culture-aware command wrapper
function Use-Culture {
param (
[System.Globalization.CultureInfo]$culture = (throw "USAGE: Use-Culture -Culture culture -Script {scriptblock}"),
[ScriptBlock]$script = (throw "USAGE: Use-Culture -Culture culture -Script {scriptblock}")
)
$OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
trap {
[System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
}
[System.Threading.Thread]::CurrentThread.CurrentCulture = $culture