Skip to content

Instantly share code, notes, and snippets.

View mattcargile's full-sized avatar

Matt Cargile mattcargile

  • Baton Rouge, LA
View GitHub Profile
@romero126
romero126 / Import-ModuleManually
Created July 19, 2023 18:20
Import-Module Manually
function Import-ModuleManually {
param(
[Parameter(Mandatory=$true)]
[string]$Module,
[Parameter(Mandatory=$false)]
[switch]$Force
)
$moduleExists = Get-Module -Name $Module -All
@Jaykul
Jaykul / Grouping.psm1
Created April 14, 2023 22:18
Expand and Group objects when they have (or could be deduped with) a single array property
filter Expand-Property {
<#
.SYNOPSIS
Expands an array property, creating a duplicate object for each value
.EXAMPLE
[PSCustomObject]@{ Name = "A"; Value = @(1,2,3) } | Expand-Property Value
Name Value
---- -----
A 1
@JustinGrote
JustinGrote / README.MD
Last active September 6, 2024 04:02
A proxy for Format-Table to apply the resultant view or save it as a format definition

TypeFormat

This module provides an improved Format-Table that lets you persist the resultant Format-Table view either to the current session or to a .ps1xml formatting file.

This module requires PowerShell 7.2+, however the generated XML format files can be used with earlier versions.

Quick Start

PS> Get-Date
using module ActiveDirectory
using namespace System.Reflection
function Convert-ADFilter {
<#
.SYNOPSIS
Converts PowerShell-style filters used by the AD module into LDAP filters.
.DESCRIPTION
Convert-ADFilter uses the QueryParser from the AD module to convert PowerShell-style filters into LDAP
@santisq
santisq / Get-TreeOrganizationalUnit.ps1
Last active November 29, 2022 21:59
tree function for AD OUs and Containers
using namespace System.Collections.Generic
using namespace Microsoft.ActiveDirectory.Management
using namespace System.Management.Automation
function Get-TreeOrganizationalUnit {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string] $Identity,
@jborean93
jborean93 / Get-WTSSessionInfo.ps1
Last active March 26, 2024 14:49
Tries to replicate qwinsta but return structured objects
# Copyright: (c) 2022, Jordan Borean (@jborean93) <[email protected]>
# MIT License (see LICENSE or https://opensource.org/licenses/MIT)
Function Get-WTSSessionInfo {
<#
.SYNOPSIS
Enumerates sessions on a Windows host.
.DESCRIPTION
Enumerates all the sessions available on a Windows host through the WTSEnumerateSessionsExW API.
@Jaykul
Jaykul / Computers.csv
Last active June 25, 2022 22:48
An Invoke Wrapper For Labs
ComputerName Email UserName Password
NotOne [email protected] bob s3cr3t
NotTwo [email protected] sue m04rs3cr3t
@Jaykul
Jaykul / About joining strings.md
Last active June 28, 2022 02:07
StringBuilder vs += vs -join @()

TL;DR: Use StringBuilder.

The truth: unless you're joining large amounts of long strings, the time it'll take PowerShell to read, parse, and compile the file is going to outweigh any improvements you make in the runtime unless you run the code repeatedly. This is easy to forget about, but try it for yourself. Download and run Strings.ps1 below, and then, run it a second time -- remember, you incur that first run penalty in each new PowerShell session.

image

For dozens to hundreds of strings, StringBuilder is only microseconds faster than +=

Obviously the results vary depending on your strings! The more there are, and the longer they are, the bigger gain you get from using StringBuilder. To sum up: StringBuilder is faster except in very small test cases, but it's not much faster except in extremely large test cases.

function Get-ComputerSession {
[CmdletBinding()]
param(
# The computer name to get the current sessions from. Use an empty string for local machine.
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[AllowEmptyString()]
[String]
$ComputerName,
# Flattens the output
@jborean93
jborean93 / win_powershell_ssh.ps1
Last active January 8, 2025 10:50
Windows PowerShell SSH Remoting Stub
<#
.SYNOPSIS
Windows PowerShell SSH Server Subsystem Shim.
.DESCRIPTION
Used as a basic wrapper for Windows PowerShell that allows it to be used as a target for SSH based remoting sessions.
This allows a PowerShell client to target a Windows host through SSH without having PowerShell 7 installed.
.NOTES
This is experimental and used as a POC.