powershell_cheat_sheet
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
powershell_cheat_sheet [2013/09/30 20:48] – [Text Processing] stephen | powershell_cheat_sheet [2021/04/13 21:53] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Powershell Cheat Sheet ====== | ====== Powershell Cheat Sheet ====== | ||
+ | |||
+ | ===== File exists check ===== | ||
+ | |||
+ | <code powershell> | ||
+ | if (Test-Path $path) { | ||
+ | Write-Host "File exists." | ||
+ | } | ||
+ | if (!(Test-Path $path)) { | ||
+ | Write-Host "File does not exist." | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Check previous command was successful ===== | ||
+ | |||
+ | <code powershell> | ||
+ | if ($?) { | ||
+ | Write-Host ' | ||
+ | } else { | ||
+ | Write-Host ' | ||
+ | throw ' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Searching in files ===== | ||
+ | |||
+ | <code powershell> | ||
+ | Get-ChildItem -Recurse -Path C:\Temp -Include *.txt | Select-String ' | ||
+ | </ | ||
+ | |||
+ | ===== Search and replace in a file ===== | ||
+ | |||
+ | <code powershell> | ||
+ | (Get-Content $filename) -replace ' | ||
+ | </ | ||
+ | |||
+ | ===== Create a timestamp ===== | ||
+ | |||
+ | <code powershell> | ||
+ | $timestamp = (Get-Date).ToString(" | ||
+ | </ | ||
+ | |||
+ | ===== Parameters ===== | ||
+ | |||
+ | <code powershell> | ||
+ | param ( | ||
+ | [parameter(Mandatory=$true, | ||
+ | [parameter(Mandatory=$true)] [string]$targetWebConfig, | ||
+ | [parameter(Mandatory=$false)] [string]$build = ' | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | ===== Set current directory to script location ===== | ||
+ | |||
+ | <code powershell> | ||
+ | Push-Location " | ||
+ | #... | ||
+ | Pop-Location | ||
+ | </ | ||
+ | |||
+ | ===== Certificates ===== | ||
+ | |||
+ | Search by thumbprint: | ||
+ | |||
+ | <code powershell> | ||
+ | Get-ChildItem -Path Cert: -Recurse | Where-Object { $_.Thumbprint -and $_.Thumbprint.ToLower().StartsWith(' | ||
+ | </ | ||
+ | |||
+ | To check if the cert is valid, pipe it the '' | ||
+ | |||
+ | <code powershell> | ||
+ | Get-ChildItem -Path Cert: -Recurse | Where-Object { $_.Thumbprint -and $_.Thumbprint.ToLower().StartsWith(' | ||
+ | </ | ||
+ | |||
+ | To check for expired / revoked certs: | ||
+ | |||
+ | <code powershell> | ||
+ | $certs = Get-ChildItem -Path Cert: | ||
+ | foreach ($cert in $certs) { | ||
+ | Write-Host " | ||
+ | |||
+ | if ($certs.NotAfter -lt (Get-Date)) { | ||
+ | Write-Host " | ||
+ | } | ||
+ | elseif ($cert | Test-Certificate) { | ||
+ | Write-Host " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Get Install-Module working behind a webproxy ===== | ||
+ | |||
+ | - Run '' | ||
+ | - Add this to the top:< | ||
+ | # Uncomment this to use PowerShellGet | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | - Uncomment, fill out the details, and save. | ||
+ | - Start a //new// Powershell session. | ||
+ | - Run '' | ||
+ | - Run '' | ||
+ | - Once installation is complete, comment out commands and change password. | ||
+ | |||
+ | If this doesn' | ||
+ | |||
+ | ===== No Sleep ===== | ||
+ | |||
+ | May only work inside Windows Powershell ISE. | ||
+ | |||
+ | <code powershell> | ||
+ | $intervalSeconds = 60 | ||
+ | $mouseDistance = 1 | ||
+ | |||
+ | $myshell = New-Object -com " | ||
+ | while ($true) { | ||
+ | Start-Sleep -Seconds $intervalSeconds | ||
+ | $Pos = [System.Windows.Forms.Cursor]:: | ||
+ | [System.Windows.Forms.Cursor]:: | ||
+ | [System.Windows.Forms.SendKeys]:: | ||
+ | Write-Host 'A shuffle to the right.' | ||
+ | Start-Sleep -Seconds $intervalSeconds | ||
+ | $Pos = [System.Windows.Forms.Cursor]:: | ||
+ | [System.Windows.Forms.Cursor]:: | ||
+ | [System.Windows.Forms.SendKeys]:: | ||
+ | Write-Host 'A shuffle to the left.' | ||
+ | } | ||
+ | </ | ||
===== Text Processing ===== | ===== Text Processing ===== | ||
+ | ==== Simple replace (supports regexs) ==== | ||
<code powershell> | <code powershell> | ||
- | # Simple replace (supports regexs) | ||
' | ' | ||
+ | </ | ||
- | # Filter lines by regex. | + | ==== Simple match (supports regexs) ==== |
+ | <code powershell> | ||
+ | ' | ||
+ | $Matches.Values | ||
+ | </ | ||
+ | |||
+ | ==== Filter lines by regex ==== | ||
+ | <code powershell> | ||
$inputText = '!! Match me | $inputText = '!! Match me | ||
Not me | Not me | ||
Line 14: | Line 151: | ||
$regex=[regex] ' | $regex=[regex] ' | ||
$regex.matches($inputText) | Foreach-Object { $_.Value } # Returns a string, one match per line. | $regex.matches($inputText) | Foreach-Object { $_.Value } # Returns a string, one match per line. | ||
+ | </ | ||
- | # Reformatting by using regex groups | + | ==== Reformatting by using regex groups |
+ | |||
+ | <code powershell> | ||
# Filter lines by regex. | # Filter lines by regex. | ||
$inputText = ' .. . . left:10 . .. right:10 ... | $inputText = ' .. . . left:10 . .. right:10 ... | ||
Line 31: | Line 171: | ||
</ | </ | ||
+ | ==== Regex groups ==== | ||
- | ===== Searching in files ===== | + | <code powershell> |
+ | $regex=[regex] (' | ||
+ | $input=' | ||
+ | $matches | ||
+ | if (!($matches.Success)) { | ||
+ | Write-Host "Regex failed to match: $input" | ||
+ | Exit(-1) | ||
+ | } | ||
+ | Write-Host " | ||
+ | Write-Host " | ||
+ | Write-Host " | ||
+ | </ | ||
+ | |||
+ | ==== Regex replace with groups | ||
<code powershell> | <code powershell> | ||
- | Get-ChildItem -Recurse -Path C:\Temp -Include *.txt | Select-String | + | # $1 = group 1, $2 = group 2, etc. |
+ | '123 456 789' | ||
</ | </ | ||
+ | ==== Pipeline function ==== | ||
+ | |||
+ | <code powershell> | ||
+ | function Replace-String { | ||
+ | [cmdletbinding()] | ||
+ | Param ( | ||
+ | [parameter(ValueFromPipeline)] | ||
+ | [string]$Input, | ||
+ | [string]$OldValue, | ||
+ | [string]$NewValue | ||
+ | ) | ||
+ | |||
+ | Process { | ||
+ | $Input -replace $OldValue, $NewValue | ||
+ | } | ||
+ | } | ||
+ | |||
+ | ' | ||
+ | </ |
powershell_cheat_sheet.1380574133.txt.gz · Last modified: 2017/01/01 19:50 (external edit)