-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowershell-quick-reference.html
More file actions
193 lines (170 loc) · 9.2 KB
/
powershell-quick-reference.html
File metadata and controls
193 lines (170 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PowerShell Quick Reference</title>
<style>
/* Set page size to Tabloid (ANSI B) Landscape */
@page {
size: tabloid landscape;
margin: 0.5in;
}
body {
font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, Roboto, Arial, sans-serif;
font-size: 11px;
line-height: 1.4;
color: #000000; /* Updated to pure black for maximum contrast */
margin: 0;
padding: 0;
}
.header {
text-align: center;
margin-bottom: 20px;
padding-bottom: 10px;
border-bottom: 2px solid #012456;
}
.header h1 {
color: #012456;
margin: 0 0 5px 0;
font-size: 24px;
}
.golden-rule {
background-color: #e8f4f8;
border-left: 4px solid #012456;
padding: 10px;
margin-bottom: 15px;
font-style: italic;
color: #000000;
}
/* 3-Column Layout for the Quick Reference */
.container {
column-count: 3;
column-gap: 30px;
}
.section {
break-inside: avoid;
margin-bottom: 20px;
}
h3 {
color: #012456;
border-bottom: 2px solid #999; /* Darkened border */
padding-bottom: 3px;
margin-top: 0;
font-size: 14px;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 10px;
font-size: 10px;
}
th, td {
border: 1px solid #999; /* Darkened border for better print visibility */
padding: 6px;
text-align: left;
vertical-align: top;
}
th {
background-color: #e2e2e2; /* Slightly darker gray for header background */
font-weight: bold;
color: #012456;
}
code {
font-family: 'Consolas', 'Courier New', monospace;
background-color: #f4f4f4;
padding: 2px 4px;
border-radius: 3px;
font-size: 9.5px;
color: #8b0000; /* Deepened to Dark Red for higher contrast */
font-weight: bold; /* Added bold to make code pop */
white-space: pre-wrap;
}
ul {
margin: 0;
padding-left: 20px;
}
li {
margin-bottom: 4px;
}
</style>
</head>
<body>
<div class="header">
<h1>PowerShell Quick Refernce</h1>
</div>
<div class="golden-rule">
<strong>The Golden Rule of PowerShell:</strong> Posix shells pipe <strong>text strings</strong>; PowerShell pipes <strong>objects</strong> (structured data with properties). Many familiar Bash commands (`ls`, `cat`, `rm`) work in PowerShell out of the box because they are built-in aliases for PowerShell cmdlets.
</div>
<div class="container">
<div class="section">
<h3>Key Syntax: Spaces vs. Commas</h3>
<p>In Bash, passing multiple arguments is done using <strong>spaces</strong>. In PowerShell, multiple values form an array, constructed using <strong>commas</strong>.</p>
<table>
<tr><th>Scenario</th><th>Bash (Spaces)</th><th>PowerShell (Commas)</th></tr>
<tr><td>List multiple dirs</td><td><code>ls /etc /var</code></td><td><code>ls C:\Windows, C:\Temp</code></td></tr>
<tr><td>Delete files</td><td><code>rm f1.txt f2.txt</code></td><td><code>rm f1.txt, f2.txt</code></td></tr>
<tr><td>Kill PIDs</td><td><code>kill 123 456</code></td><td><code>Stop-Process 123, 456</code></td></tr>
</table>
</div>
<div class="section">
<h3>File & Directory Navigation</h3>
<table>
<tr><th>Bash</th><th>PowerShell Cmdlet</th><th>Alias</th><th>Example</th></tr>
<tr><td><code>ls</code></td><td><code>Get-ChildItem</code></td><td><code>ls</code>, <code>dir</code></td><td><code>ls -Force</code> (-a)</td></tr>
<tr><td><code>cd</code></td><td><code>Set-Location</code></td><td><code>cd</code>, <code>sl</code></td><td><code>cd /path</code></td></tr>
<tr><td><code>cat</code></td><td><code>Get-Content</code></td><td><code>cat</code>, <code>gc</code></td><td><code>cat file.txt</code></td></tr>
<tr><td><code>rm</code></td><td><code>Remove-Item</code></td><td><code>rm</code>, <code>del</code></td><td><code>rm -Force file.txt</code></td></tr>
<tr><td><code>cp</code></td><td><code>Copy-Item</code></td><td><code>cp</code></td><td><code>cp f1.txt f2.txt</code></td></tr>
<tr><td><code>mv</code></td><td><code>Move-Item</code></td><td><code>mv</code></td><td><code>mv file.txt /path/</code></td></tr>
<tr><td><code>touch</code></td><td><code>New-Item</code></td><td><code>ni</code></td><td><code>ni f.txt -ItemType File</code></td></tr>
</table>
</div>
<div class="section">
<h3>The Big Four: grep, find, wc, tee</h3>
<table>
<tr><th>Bash</th><th>PowerShell</th><th>Example</th></tr>
<tr><td><code>grep</code></td><td><code>Select-String</code></td><td><code>sls "error" app.log</code><br><code>cat app.log | sls "error"</code></td></tr>
<tr><td><code>grep -r</code></td><td><code>sls</code> with <code>ls</code></td><td><code>ls -Recurse -File | sls "error"</code><br><code>ls -Recurse -Filter "*.log" | sls "error"</code></td></tr>
<tr><td><code>find</code></td><td><code>Get-ChildItem</code></td><td><code>ls -Recurse -Filter "*.log"</code></td></tr>
<tr><td><code>wc -l</code></td><td><code>Measure-Object</code></td><td><code>cat app.log | Measure-Object -Line</code><br><em>Shortcut:</em> <code>(ls).Count</code></td></tr>
<tr><td><code>tee</code></td><td><code>Tee-Object</code></td><td><code>echo "hi" | tee out.txt</code><br><em>Append:</em> <code>... | tee -Append out.txt</code></td></tr>
</table>
</div>
<div class="section">
<h3>Text Manipulation (sed/awk Equivalents)</h3>
<p>Parse raw text with regex, but parse commands with <code>Select-Object</code>.</p>
<table>
<tr><th>Concept</th><th>Bash</th><th>PowerShell Equivalent</th></tr>
<tr><td>Extract column</td><td><code>ps | awk '{print $1}'</code></td><td><code>Get-Process | select Name</code><br><em>Shortcut:</em> <code>(Get-Process).Name</code></td></tr>
<tr><td>Find/Replace (Stream)</td><td><code>echo "a" | sed 's/a/b/'</code></td><td><code>"a" -replace "a", "b"</code></td></tr>
<tr><td>Find/Replace (In File)</td><td><code>sed -i 's/a/b/g' f.txt</code></td><td><code>(cat f.txt) -replace 'a','b' | Out-File f.txt</code></td></tr>
<tr><td>Split & get Nth</td><td><code>echo "a b" | awk '{print $2}'</code></td><td><code>("a b" -split '\s+')[1]</code> (0-indexed)</td></tr>
</table>
</div>
<div class="section">
<h3>Deep Dive: Where-Object (Filtering)</h3>
<p>Filter objects based on properties using <code>Where-Object</code> (aliases: <code>where</code>, <code>?</code>). <code>$_</code> represents the current object.</p>
<ul>
<li><strong>Operators:</strong> <code>-eq</code>, <code>-ne</code>, <code>-gt</code>, <code>-match</code> (regex), <code>-like</code> (wildcard)</li>
<li><strong>Filter by name:</strong> <code>Get-Process | where { $_.Name -match "chrome" }</code></li>
<li><strong>Filter files > 1MB:</strong> <code>ls | where { $_.Length -gt 1MB }</code></li>
<li><strong>Multiple:</strong> <code>Get-Process | where { $_.CPU -gt 10 -and $_.Name -notlike "*svchost*" }</code></li>
<li><strong>Simplified:</strong> <code>ls | where Length -gt 1MB</code></li>
</ul>
</div>
<div class="section">
<h3>Streams, Redirection & Bash-isms</h3>
<table>
<tr><th>Concept</th><th>Bash</th><th>PowerShell</th></tr>
<tr><td>To <code>/dev/null</code></td><td><code>cmd > /dev/null</code></td><td><code>cmd > $null</code> <br><em>or</em> <code>cmd | Out-Null</code></td></tr>
<tr><td>Overwrite File</td><td><code>cmd > f.txt</code></td><td><code>cmd > f.txt</code></td></tr>
<tr><td>Append File</td><td><code>cmd >> f.txt</code></td><td><code>cmd >> f.txt</code></td></tr>
<tr><td>Error to Null</td><td><code>cmd 2> /dev/null</code></td><td><code>cmd 2> $null</code></td></tr>
<tr><td>Error to Output</td><td><code>cmd 2>&1</code></td><td><code>cmd 2>&1</code></td></tr>
<tr><td>Discard ALL</td><td><code>cmd > /dev/null 2>&1</code></td><td><code>cmd *> $null</code></td></tr>
<tr><td>AND / OR</td><td><code>c1 && c2 || c3</code></td><td><code>c1 && c2 || c3</code> (PS v7+)</td></tr>
</table>
</div>
</div>
</body>
</html>