Skip to content

Instantly share code, notes, and snippets.

@62mkv
Last active March 15, 2022 15:00
Show Gist options
  • Save 62mkv/2fd8927b9b956b2a35b561c7b3425cdb to your computer and use it in GitHub Desktop.
Save 62mkv/2fd8927b9b956b2a35b561c7b3425cdb to your computer and use it in GitHub Desktop.
PowerShell cheatsheet

Getting help on any topic:

help _topic_

or

man _topic_

Read all lines from text file

$lines = Get-Content -Path 'C:\USER\Documents\Collections\collection.txt'

Extract value from each line using regexp (and assign result to variable)

$lines | foreach-object {$_ -replace '^\d+\)\s+\"(.+?):.*\"$', '$1'} | Tee-Object -Variable keys

Group string values and count occurrences

$values | Group-Object

View list of all available commands in interactive grid with filtering:

Get-Command | Out-GridView

Change dir to temp folder:

cd $Env:TEMP

Load JSON file into object

$j = Get-Content -Raw -Path <file-path> | ConvertFrom-Json 

Extract data from JSON Object as PowerShell array

$arr = @(Foreach ($e in $j.data.entries) { $e.columns[3].message})

Write data to file

$arr | Out-File <filename>

List filenames, corresponding to a mask, in current folder

Get-ChildItem -Filter '*.data' -Name

List filenames and use replace on the names

(gci -filter *.data) |%{ $_ -replace '.data',""} | %{Echo "certutil -decode $_.data $_.zip"}

Base64-decode all files in a folder

foreach ($file in (gci *.data)) {
  $content = get-content $file;
  $decoded = [System.Convert]::FromBase64String($content);
  $name = $file.Name -replace ".data",".bzip2"
  set-content $name -Value $decoded -Encoding Byte
}

Load library from file

[System.Reflection.Assembly]::LoadFrom("c:\\dinoch\\bin\\Ionic.Zip.dll");
$zipfile =  new-object Ionic.Zip.ZipFile;
$zipfile.GetType();

Show all constructor overloads

[System.io.FileStream]::New
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment