-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-GettingStarted.ps1
127 lines (82 loc) · 1.72 KB
/
01-GettingStarted.ps1
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
# Variable assignment
$a = 1
$b = 2
$c = $a + $b
$d = "test"
$a + $d
# Common Types
$a = "String"
$array = @("one", "two", "three")
$array2 = @("red", "white", "blue")
$complexArray = @($array, $array2)
$array[0]
$array[1]
$complexArray[0][1]
$hash = @{
a = "test";
b = "snap";
}
$hash["a"]
$bool = $true
$bool2 = $false
# Built in CmdLets
Get-Process
Get-Service
Get-Disk
# Loops
for ($i=0;$i -lt 3; $i++)
{
$i
}
foreach ($a in 1..5)
{
Write-Host $a -ForegroundColor White
}
while($i -lt 4)
{
$i
$i++
}
do {
$i
} while ($i -lt 2)
# if and switch
if($i -eq 4)
{
Write-Host "This only prints with 4" -ForegroundColor Yellow
}
switch( $i )
{
1 {
Write-Host "Red" -ForegroundColor Red
}
4 {
Write-Host "Blue" -ForegroundColor Blue
}
default {
Write-Host "Green" -ForegroundColor Green
}
}
# Regular expressions and looking for strings
Select-String -Path .\01-GettingStarted.ps1 -Pattern '\$i'
"test" -match "es"
"test" -match "esk"
# Looking for files
Get-ChildItem 'C:\Program Files' -Depth 0
Get-ChildItem 'C:\Program Files' -Depth 1 -Include "*.exe" | Select -first 10 | Select -Property Name
Get-ChildItem 'C:\Program Files' -Depth 1 -Include "*.exe" | Select -first 10 | Select -ExpandProperty Name
Get-ChildItem 'C:\Program Files' -Depth 1 -Include "*.exe" | Select -first 10 | Select -ExpandProperty Name | Sort-object
# Saving to a file
Get-Service | ogv
Get-Service | Out-GridView -PassThru | Out-file c:\temp\MyServicesSelected.txt
# Functions
function Get-CService {
Get-Service | Sort-Object Status
}
function Get-MyService {
param(
$Name
)
Get-Service -Include "$Name"
}
# Build a logging function