Skip to content

Instantly share code, notes, and snippets.

@instance-id
Last active August 3, 2022 20:24
Show Gist options
  • Save instance-id/38829e4efb4fea8063d9ec90b4196d30 to your computer and use it in GitHub Desktop.
Save instance-id/38829e4efb4fea8063d9ec90b4196d30 to your computer and use it in GitHub Desktop.
PowerShell Snippets I don't want to forget.
# -- String aggregation from an array of PSObject ---------------------------------
# -- Takes an Array: -------------------------------
$array = @("string1","string2","string3","string4","string5")
[Func[string, string, string]]$aggregator = { param ($first, $next) $first + "${next}, "; };
$output = [System.Linq.Enumerable]::Aggregate([string[]]$array, [string]::Empty, $aggregator);
# -- Produces a string: ----------------------------
Write-Host $output
# -- $_> string1, string2, string3, string4, string5
# --------------------------------------------------------------------------------
# -- Alias Helper -----------------------------------------------------------------
# --------------------------------------------------------------------------------
# -- Lets you create Alias in one line instead of having to
# -- declare a function and alias separetly for each one.
function Set-AliasCommand {
[CmdletBinding()]param([Parameter(Mandatory = $true, Position = 0)][String]$Alias,[Parameter(Mandatory = $true, Position = 1)][String]$Command)
New-Item -Path function:\ -Name "global:$Alias" -Value $($Command) -Force | Out-Null
} New-Alias -Name newalias -Value Set-AliasCommand -Force -Option AllScope
# -- Example Usage ---------------------------------
ac psadmin 'Start-Process -Verb RunAs (Get-Process -Id $PID).Path'
ac la 'Get-ChildItem | Sort-Object -Property LastWriteTime'
# --------------------------------------------------------------------------------
@instance-id
Copy link
Author

I just lol'd. I was trying to figure out how to aggregate a string array and I came across my own posting, which I had forgotten I even made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment