@@ -14,12 +14,21 @@ param()
1414 Each command should use a verb appropriate to the action it's taking, and a
1515 noun that succinctly and clearly describes what it's acting upon.
1616
17- In this topic, we'll cover the a few of the most common verbs which you'll
18- see most frequently.
17+ In this topic, we'll cover a few of the most common verbs which you'll see
18+ most frequently.
1919#>
2020
2121Describe " Basic Verbs" {
2222
23+ BeforeAll {
24+ # We'll be using this path later on.
25+ $FilePath = " $env: TMP /YOUR_PATH.txt"
26+
27+ if (Test-Path $FilePath ) {
28+ Remove-Item - Path $FilePath
29+ }
30+ }
31+
2332 Context " Get" {
2433 <#
2534 Cmdlets with the Get verb are used for retrieving data.
@@ -78,14 +87,28 @@ Describe "Basic Verbs" {
7887 <#
7988 Let's try creating a file!
8089
81- $Path = "YOUR PATH.txt"
82- New-Item -Path $Path -ItemType file
90+ Use New-Item to create a new text file, in the location
91+ specified with the $FilePath variable, which is defined above.
8392
84- This will create a new text file, in the location you specify.
85- In this case, we'll
93+ The necessary parameters are already filled in for you here. In
94+ this case, the file we're creating is simply empty. You could
95+ also specify a -Value parameter with some text to put in the
96+ newly-created file.
97+
98+ Take some time to experiment with New-Item in your console if
99+ you'd like to see what it can do! Start with:
100+
101+ Get-Command New-Item -Syntax
102+ Get-Help New-Item -Examples
86103 #>
87104
88- $File
105+ $File = ____ - Path $FilePath - ItemType File
106+
107+ # All "file" objects are of this type.
108+ $File | Should - BeOfType [System.IO.FileInfo ]
109+
110+ # An empty file has a "length" of zero.
111+ $File.Length | Should - Be 0
89112 }
90113 }
91114
@@ -94,19 +117,6 @@ Describe "Basic Verbs" {
94117 Cmdlets with the Add verb append data to an existing object or data
95118 source.
96119
97- If you followed the example in the 'new' test, you can use
98- Add-Content to add some text to your newly created text file:
99-
100- $Path = "YOUR PATH.txt"
101- $text = "Sgt. Bash is the best house robot because... fire."
102- Add-Content -Path $Path -Value $text
103-
104- Before continuing, run this command several times. See what happens;
105- is it the result you expected?
106-
107- You'll see that several lines of text were added to the file. This
108- cmdlet only appends information, it does not overwrite.
109-
110120 Essentially, if the target doesn't exist then a cmdlet with the Add
111121 verb will typically create it. If it does exist, the cmdlet will
112122 add data to it, if data can be added without overwriting the
@@ -128,27 +138,54 @@ Describe "Basic Verbs" {
128138
129139 $Answers | Get-Unique | Should - HaveCount 5 - Because " five unique cmdlets are required"
130140 }
141+
142+ It ' can Add-Content to a file' {
143+ <#
144+ Try adding this content to the file we created above using
145+ Add-Content.
146+ #>
147+ " Mountains are merely mountains." | ____ - Path $FilePath
148+
149+ <#
150+ Let's see what happens if we add a whole bunch of things! Fill
151+ in these blanks with whatever you like.
152+ #>
153+
154+ ' ____' | Add-Content - Path $FilePath
155+ ' ____' | Add-Content - Path $FilePath
156+ ' ____' | Add-Content - Path $FilePath
157+ ' ____' | Add-Content - Path $FilePath
158+
159+ # Let's check the contents of the file.
160+ $FileData = Get-Content - Path $FilePath
161+
162+ # How many lines did we end up with?
163+ __ | Should - Be $FileData.Count
164+
165+ <#
166+ We can see that several lines of content were added to the file.
167+ Add-* cmdlets can only append data, they can't overwrite. With
168+ this information, you should be able to determine what the
169+ expected content of the file is at this point.
170+ #>
171+
172+ $ExpectedContent = @ (
173+ ' Mountains are merely mountains.'
174+ ' ____'
175+ ' ____'
176+ ' ____'
177+ ' The road onwards, the road back; which is the shorter?'
178+ )
179+
180+ $ExpectedContent | Should - BeExactly $FileData
181+ }
131182 }
132183
133184 Context " Set" {
134185 <#
135186 Cmdlets with the Set verb will overwrite information that already
136187 exists.
137188
138- If you followed the example in the 'New' and 'Add' tests, you can
139- use set to do something to your text file:
140-
141- $Path = "YOUR PATH.txt"
142- $text = "Sir Kill-A-Lot is the best house robot because of reasons."
143- Set-Content -Path $Path -Value $text
144-
145- Before continuing, run this command several times. See what happens;
146- is it the result you expected?
147-
148- You'll see that there's only one line of text in the file. This is
149- because the Set-Content command will overwrite information that's
150- already there.
151-
152189 Some Set-* cmdlets require the instance to already be present for
153190 you to change it; you'll need to use a New-* cmdlet first to create
154191 an instance before you can overwrite information within it.
@@ -170,42 +207,83 @@ Describe "Basic Verbs" {
170207
171208 $Answers | Get-Unique | Should - HaveCount 5 - Because " five unique cmdlets are required"
172209 }
210+
211+ It ' can Set-Content for a file' {
212+ <#
213+ Let's try using Set-Content on our text file from before. But
214+ first, let's check that it still has the contents we added. We
215+ should still have 5 lines in it from before.
216+
217+ If you added extra lines to the file in the Add-Content koan
218+ above, make sure to update the expected line count here!
219+ #>
220+ $LineCount = 5
221+ Get-Content - Path $FilePath | Should - HaveCount $LineCount
222+
223+ # Now let's try setting the contents.
224+ " Wherever you are, it's the place you need to be." | Set-Content - Path $FilePath
225+
226+ # So what should be in the file now?
227+ $FileContent = Get-Content - Path $FilePath
228+ " ____" | Should - BeExactly $FileContent
229+
230+ # What happens if we set the contents again?
231+ ' ____' | Set-Content - Path $FilePath
232+ Get-Content - Path $FilePath | Should - BeExactly ' Rest and be kind, you don’ t have to prove anything.'
233+ <#
234+ You' ll see that there' s only one line of text in the file. This
235+ is because the Set-Content command will completely overwrite
236+ whatever is in the file already.
237+ #>
238+ }
173239 }
174240
175241 Context "Remove" {
176242 <#
177243 Cmdlets with the Remove verb will delete data from an object or data
178244 source.
179245
180- If you followed the example in the 'New','Add' and 'Set' tests, you
181- can use Remove-Item to delete your text file:
182-
183- $Path = "YOUR PATH.txt"
184- Remove-Item -Path $Path
185-
186- Before continuing, run this command a few times. What happens when
187- you try to run it once the item has been deleted?
188-
189- You'll see that it fails. Cmdlets with the verb Remove simply remove
190- data; if the instance you're referring to doesn't exist, then
191- there's nothing available to remove and it will emit an error
192- message.
193-
194- Returning to Calendar permissions in Office 365, you can use a
246+ Once again with calendar permissions in Office 365, you can use a
195247 Remove cmdlet to completely remove a user' s permissions to a
196248 calendar.
197- #>
249+ # >
198250
199- It " is for commands that delete data" {
200- <#
251+ It " is for commands that delete data" {
252+ <#
201253 Using Get-Command, find 5 commands with the Remove verb.
202254
203255 Replace each ____ with the name of a Remove-* command.
204256 #>
205- $Answers = " ____" , " ____" , " ____" , " ____" , " ____"
206- $Answers | Should - BeIn (Get-Command - Verb Remove).Name
207-
208- $Answers | Get-Unique | Should - HaveCount 5 - Because " five unique cmdlets are required"
257+ $Answers = " ____" , " ____" , " ____" , " ____" , " ____"
258+ $Answers | Should - BeIn (Get-Command - Verb Remove).Name
259+
260+ $Answers | Get-Unique | Should - HaveCount 5 - Because " five unique cmdlets are required"
261+ }
262+
263+ It ' can Remove-Item to delete a file' {
264+ <#
265+ We can use Remove-Item to delete the text file we've been
266+ working with. Before we do, let's just double check the file
267+ still exists. 'Leaf' here refers to a file; 'Container'
268+ would be the corresponding type for a folder.
269+ #>
270+ Test-Path $FilePath - PathType Leaf | Should - BeTrue
271+
272+ # Pester has its own way of checking that files exist.
273+ $FilePath | Should - Exist
274+
275+ # Use Remove-item to delete the file completely.
276+ ____ - Path $FilePath
277+
278+ # Let's check it was removed properly. Test-Path $FilePath -PathType Leaf | Should -BeTrue
279+ Test-Path $FilePath | Should - BeFalse
280+
281+ <#
282+ If we try to remove a file that doesn't exist, we should get
283+ an error. What does that error look like?
284+ #>
285+ $Message = " ____"
286+ { Remove-Item - Path $FilePath - ErrorAction Stop } | Should - Throw - ExpectedMessage $Message
287+ }
209288 }
210289 }
211- }
0 commit comments