-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path1.2-Variables.ps1
More file actions
97 lines (72 loc) · 2.42 KB
/
Copy path1.2-Variables.ps1
File metadata and controls
97 lines (72 loc) · 2.42 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
#First, clear the screen
Clear-Host
#Variables and using variables
Get-Help about_variables -ShowWindow
#Powershell variables start with a $
Clear-Host
$string="This is a variable"
$string
#We can use Get-Member to find out all the information on our objects
$string | Get-Member
$string.Length
$string.IndexOf('s')
#Powershell is strongly typed and uses .Net objects.
#Not just limited to strings and intgers
$date=Get-Date
$date
$date | gm #gm is the alias of Get-Member
#Because they are .Net types/classes, we can use the methods and properties.
$date.Day
$date.DayOfWeek
$date.DayOfYear
$date.ToUniversalTime()
#Powershell tries to figure out the variable type when it can(implicit types)
#We can also explicitly declare our type
[string]$datestring = Get-Date #could also use [System.String]
$datestring
$datestring|gm
#EVERYTHING is an object. This means more than just basic types:
$file = New-Item -ItemType File -Path 'C:\TEMP\junkfile.txt'
$file | gm
$file.Name
$file.FullName
$file.Extension
$file.LastWriteTime.ToUniversalTime()
Remove-Item $file
#Concatenation and Interpolation
#The plus sign is used for concatenation
$temperature = 'Hot'
'Tea. Earl Grey. ' + $temperature + '.'
#We can use interpolation to make life easier and cleaner.
#Interpolation is a useful tool when working with variables, especially strings.
"Tea. Earl Grey. $temperature."
'Tea. Earl Grey. $temperature.'
#It is important to understand the difference between single and double quotes.
#` (the tick on the tilde key) is the escape character, use this when you need to get around special characters
"Tea. Earl Grey. `$temperature."
#cmdlets and functions will output objects. You can work with them by using ()
$GCIDemo = Get-ChildItem C:\Windows
$GCIDemo.Length
(Get-ChildItem C:\Windows).Length
(Get-Date).AddDays(-3)
#Get-Help will give you the object type that the cmdlet outputs
#Arrays and collections
#Create a collection of commands starting with 'New'
$commands = Get-Command 'New*'
$commands.GetType()
$commands[0].GetType().ToString()
$commands | gm
$commands.Count
$commands[5] | gm
#You can create your own arrays
$commandarray = @('Make','It','So')
$commandarray
#You can merge and array with -Join
$commandarray -join ';'
($commandarray -join "`n").GetType()
#you can separate a string into an array with -split
$splitstring = 'Kirk,McCoy,Spock,Scotty' -split ','
$splitstring
$splitstring | gm
$splitstring.GetType()
$splitstring.Count