How can we help?
Handy Powershell snippits
Short and Handy to know
Aliassen and special characters
- % alias foreach
- ? alias where-object
- @ hashtable
- gsv alias Get-Service
Alias to see them all
1 2 |
cd alias: ls |
Arrays and Generic Lists
Powershell: Everything you wanted to know about arrays (powershellexplained.com)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$List = [System.Collections.Generic.List[Object]]::new(); $ListItem = [PSCustomObject][Ordered]@{ RemotePowerShellEnabled = $gu.RemotePowerShellEnabled IsSetToFalse = $gu.IsSetToFalse UserPrincipalName = $gu.UserPrincipalName DisplayName = $gu.DisplayName } #end item $List.Add($ListItem) |
Testing for empty string or null value
1 2 3 |
if ([string]::IsNullOrWhiteSpace($LastItemAddedtoTeams) -and [string]::IsNullOrEmpty($LastItemAddedtoTeams)) { } |
Hashtables and Herestring (String literal)
Hashtable
1 |
$hash = @{ Number = 1; Shape = "Square"; Color = "Blue"} |
Herstring
1 2 3 4 |
$css = @” <style> Body {enz……} ”@ |
Function example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<# [CmdletBinding()] supports common paramaters like verbose and confirm Position=0 first value with nog parameter is used as the first input needed ValueFromPipeline ValueFromPipelineByPropertyName If we want to Pipe | something we need this #> function Set-CapSettings{ [CmdletBinding()] param ( [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, HelpMessage="If something is wrong this will be shown")] [string]$NameVariable ) begin { Write-Verbose "Begin begins" } process { Write-Verbose "Processing Process" } end { Write-Verbose "Ending things" } } Set-CapSettings "Captain" -Verbose |
Working With data in Powershell
Pluralsight Working with Data in Powershell
1 2 3 |
$htmlreport = $data | convert-html -head $css -Title “Mooie titel” -precontent “<h1>Mooie header</h1>” -postcontent “<p>report generated on $(Get-Date)</p>” Start-proces $htmlreport |
Introduction · Chart.js documentation (chartjs.org)