User Tools

Site Tools


powershell_cheat_sheet

This is an old revision of the document!


Powershell Cheat Sheet

Text Processing

# Simple replace (supports regexs)
'1234abcd' -replace '\d', '*' # ****abcd
 
# Filter lines by regex.
$inputText = '!! Match me
Not me
!! Match me too
Not me tho'
$regex=[regex] '(?m)^!!.+?$'
$regex.matches($inputText) | Foreach-Object { $_.Value } # Returns a string, one match per line.
 
# Reformatting by using regex groups
# Filter lines by regex.
$inputText = ' .. . . left:10 .  .. right:10 ... 
 .. . . left:20 .  .. right:20 ... 
 .. . left:30 . . ...  .. right:30 ... 
 left:40right:40' -replace "`r`n", "`n"
 
$regex=[regex] '(?m)^[ \.]*(?<left>left:\d+)[ \.]*(?<right>right:\d+)[ \.]*$'
$regex.matches($inputText) | Foreach-Object { $_.Groups["left"].value + ' - ' + $_.Groups["right"].value}
# Returns a string:
#left:10 - right:10
#left:20 - right:20
#left:30 - right:30
#left:40 - right:40
powershell_cheat_sheet.1380513421.txt.gz · Last modified: 2017/01/01 19:50 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki