Last active
August 3, 2022 20:24
-
-
Save instance-id/38829e4efb4fea8063d9ec90b4196d30 to your computer and use it in GitHub Desktop.
PowerShell Snippets I don't want to forget.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -- 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' | |
# -------------------------------------------------------------------------------- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.