Cheat Sheet: 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);

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 " " -Authentication Bearer -Token (ConvertTo-SecureString $bearerToken -AsPlainText)

comments powered by Disqus