Cheat Sheet: PowerShell
powershell
Series of short snippets of FUC. This one is for PowerShell.
March 6, 2019
A few snippets of PowerShell I’ve found useful over and over again. Most of them are just syntax/functions that I’m unable to remember, some of them might be more complex.
It’s a living document, I keep updating it.
XML parsing
$xmlDoc = [XML](Get-Content $filePath)
$xmlDoc.Element.Subelement.setAttribute("Attr", $value);
$xmlDoc.Save($filePath)
JSON parsing
$jsonDoc = Get-Content $filePath | Out-String | ConvertFrom-Json
Convert from JSON to object and don’t shrink single-element arrays into non-array objects:
$jsonDoc = $jsonText | ConvertFrom-Json -NoEnumerate
HTTP requests
Invoke-WebRequest with authentication bearer token:
Invoke-WebRequest "https://myurl.mydomain.cz/api/test " -Authentication Bearer -Token (ConvertTo-SecureString $bearerToken -AsPlainText)