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
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) <jborean93@gmail.com>
# 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 user@gmail.com bob s3cr3t
NotTwo user2@gmail.com 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 October 15, 2023 15:14
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.
@jborean93
jborean93 / PSClassSplat.ps1
Last active December 5, 2023 10:25
Example on how to use a class as a PowerShell splat value
class SplatClass : System.Collections.IEnumerable {
SplatClass() {}
[System.Collections.IEnumerator] GetEnumerator() {
# This can be any hashtable stored or derived from the class. This is
# just an example
$params = @{
Path = '/tmp'
}