Created
February 8, 2023 13:59
-
-
Save SimonWahlin/fca00f75cdf03c39ed4275859a5d41b5 to your computer and use it in GitHub Desktop.
PowerShell Demo
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
# Save this file and import it using the command `Imort-Module '<path to file'` | |
$Script:Demo = @( | |
@' | |
$PSVersionTable | |
'@ | |
@' | |
Get-Command *AzAccount* | |
'@ | |
@' | |
Get-Command -Module Bicep | |
'@ | |
@' | |
Get-Help ConvertTo-Json | |
'@ | |
@' | |
Get-Help ConvertTo-Json -Full | |
'@ | |
@' | |
Get-Help ConvertTo-Json -Parameter Depth | |
'@ | |
@' | |
Update-Help -UICulture en-US | |
'@ | |
@' | |
Push-Location 'C:\Windows' | |
'@ | |
@' | |
Push-Location 'C:\Users' | |
'@ | |
@' | |
Pop-Location | |
'@ | |
@' | |
Get-ChildItem -File | Get-Member | |
'@ | |
@' | |
Get-ChildItem -File | |
| Where-Object Length -gt 1MB | |
| Sort-Object -Property Length -Descending | |
'@ | |
@' | |
Get-ChildItem -File | |
| Where-Object Length -gt 1MB | |
| Sort-Object -Property Length -Descending | |
| Select-Object -Property BaseName, Extension, Length | |
'@ | |
@' | |
Invoke-RestMethod -Method GET -Uri 'https://swapi.py4e.com/api/people/' | |
'@ | |
@' | |
Invoke-RestMethod -Method GET -Uri 'https://swapi.py4e.com/api/people/' | |
| Select-Object -Property results | |
'@ | |
@' | |
Invoke-RestMethod -Method GET -Uri 'https://swapi.py4e.com/api/people/' | |
| Select-Object -ExpandProperty results | |
'@ | |
@' | |
Invoke-RestMethod -Method GET -Uri 'https://swapi.py4e.com/api/people/' | |
| Select-Object -ExpandProperty results | |
| Select-Object -Property Name, Height, Mass | |
'@ | |
@' | |
az account show | |
'@ | |
@' | |
az account show | ConvertFrom-Json | |
'@ | |
@' | |
[System.DateTime]::Now | |
'@ | |
@' | |
[System.IO.Path]::Combine('one','two','three','four','five') | |
'@ | |
@' | |
[System.Collections.ArrayList]::New | |
'@ | |
@' | |
$List = [System.Collections.ArrayList]::New() | |
$List.Add('one') | |
$List | |
'@ | |
@' | |
$List = [System.Collections.ArrayList]::New() | |
$null = $List.Add('one') | |
$List | |
'@ | |
@' | |
using namespace System.Collections | |
$List = [ArrayList]::New() | |
$null = $List.Add('one') | |
$List | |
'@ | |
@' | |
$name = 'Simon' | |
'@ | |
@' | |
"My name is $name" | |
'@ | |
@' | |
"It's $names turn" | |
'@ | |
@' | |
"It's ${name}s turn" | |
'@ | |
@' | |
"The time is $(Get-Date -Format 'HH:mm')" | |
'@ | |
@' | |
help about_comparison_operators | |
'@ | |
@' | |
'name' -contains 'n' | |
'@ | |
@' | |
1,2,3 -contains 2 | |
'@ | |
@' | |
$true -eq 'true' | |
'@ | |
@' | |
$true -eq 'false' | |
'@ | |
@' | |
$array = @(1,2,3) | |
'@ | |
@' | |
$hashtable = @{ | |
MyKey = 'MyValue' | |
} | |
'@ | |
@' | |
[pscustomobject]$hashtable | |
'@ | |
) | |
function randompause | |
{ | |
$pause = Get-Random -Minimum 10 -Maximum 20 | |
Start-Sleep -Milliseconds $pause | |
} | |
function Get-DemoIndex | |
{ | |
[int][System.Environment]::GetEnvironmentVariable( | |
'DEMO_GLOBALINDEX' | |
) ?? 0 | |
} | |
function Set-Index ([int]$index = 0) | |
{ | |
[System.Environment]::SetEnvironmentVariable( | |
'DEMO_GLOBALINDEX', | |
$index | |
) | |
} | |
function Set-Demo | |
{ | |
param([string[]]$Demo) | |
$Script:Demo = $Demo | |
Set-Index | |
} | |
function Get-Demo { | |
[CmdletBinding()] | |
[Alias('d')] | |
param ( | |
[Parameter(Mandatory=$false)] | |
[int]$index | |
) | |
Clear-Host | |
if ($PSBoundParameters.ContainsKey('index')) { | |
$CurrentIndex = $index | |
} | |
else { | |
$CurrentIndex = Get-DemoIndex | |
} | |
if ($Script:Demo.Length -ge $CurrentIndex) { | |
$Script:Demo[$CurrentIndex] | Set-Clipboard | |
} | |
else { | |
Set-Clipboard -Value '' | |
} | |
Set-Index ($CurrentIndex + 1) | |
} | |
function Reset-Demo { | |
[Alias('r')] | |
param( | |
[int]$index = 0 | |
) | |
Set-Index $index | |
Clear-Host | |
} | |
try { | |
$setPSReadLineKeyHandlerSplat = @{ | |
Chord = 'ctrl+d' | |
BriefDescription = 'InsertDemoCode' | |
Description = "Insert code from clipboard for demo purpose" | |
ScriptBlock = { | |
param($key, $arg) | |
$CurrentIndex = Get-DemoIndex | |
Set-Index ($CurrentIndex + 1) | |
[string[]]$Commands = $Script:Demo[$CurrentIndex] -split [System.Environment]::NewLine | |
for ($i = 0; $i -lt $Commands.Count-1; $i++) { | |
for ($j = 0; $j -lt $Commands[$i].Length; $j++) { | |
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($Commands[$i][$j]) | |
randompause | |
} | |
# [Microsoft.PowerShell.PSConsoleReadLine]::Insert($Commands[$i]) | |
[Microsoft.PowerShell.PSConsoleReadLine]::AddLine() | |
} | |
for ($j = 0; $j -lt $Commands[$i].Length; $j++) { | |
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($Commands[-1][$j]) | |
randompause | |
} | |
# [Microsoft.PowerShell.PSConsoleReadLine]::Insert($Commands[-1]) | |
} | |
} | |
Set-PSReadLineKeyHandler @setPSReadLineKeyHandlerSplat | |
} catch {} | |
Set-Index 0 | |
# Export-ModuleMember -Function 'Get-Demo', 'Set-Demo', 'Reset-Demo', 'Get-DemoIndex' -Alias 'd', 'r' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment