Summary: In this blog, the Scripting Wife learns how to use Windows PowerShell to work with an XML file.
Microsoft Scripting Guy, Ed Wilson, is here. It is only eight days until the kickoff of the 2012 Scripting Games. Tomorrow the signup for the 2012 Scripting Games goes live. I have finished all the meetings, and everything is now in readiness state for the big event. I am sitting in the kitchen sipping a cup of English Breakfast tea, and looking over the chapter that I completed last night in my Windows PowerShell 3.0 Step by Step book. I close my eyes to think about one particular sentence when all of a sudden I feel a presence come into the room.
“If you are going to sleep, then you should get up from the table,” the Scripting Wife chided.
“I am not sleeping, I am deep in thought,” I replied.
“Then I am in deep trouble,” the Scripting Wife rejoined.
“What are you doing in here? The sun is still up,” I joked.
“Very funny. In fact, it is so funny I forgot to laugh,” she said. “Actually, since you are not doing anything, I need to you tell me about XML.”
“XML. I need to know how to read an XML file,” she repeated.
“You are kidding.”
“Nope. I exported my book database, and it saved as an XML file. I need to know how to read it,” the Scripting Wife explained.
“OK. That makes sense. In Windows PowerShell, it is really easy. How do you read a plain text file?” I asked.
“I use the Get-Content cmdlet,” she said.
“It is the same thing, except that you use the letters XML inside square brackets. Open up Windows PowerShell on your computer, and then use Get-Content to read the XML file.”
The Scripting Wife stores her books.xml file in the FSO directory on her C:\ drive. Therefore, her command looks like this:
$books = Get-Content C:\fso\books.xml
“Now look at what is contained in the $books variable,” I suggested.
The Scripting Wife typed the $books variable on its own line in the Windows PowerShell console. The command and its associated output are shown in the image that follows.
“Now look at the XML property,” I suggested.
The Scripting Wife typed the following command.
“OK. Now look at the BookInfo property.”
She typed the command that appears here.
The command to examine the XML property and the command to look at the BookInfo property are shown in the image that follows, along with the associated output.
“What is that booklist?” the Scripting Wife asked.
“I don’t know. Why don’t you access it and see.”
The Scripting Wife used the Up arrow to retrieve the previous command, and added BookList to the end of the command and pressed ENTER. Here is her command:
“Well, it looks like a bunch of books,” she replied as she turned her monitor to me so I could see. The image that follows shows her Windows PowerShell console.
“Why don’t you keep going…But this time, pipe the output to More just in case you get all of your books rolling by,” I suggested.
The Scripting Wife once again used the Up arrow to retrieve her previous command, added Book to the end of the command, and piped the output to More. The command is shown here.
$books.bookinfo.booklist.book | more
Sure enough, after a few seconds, the book information began to scroll. The first little bit is shown here.
“Well that looks interesting, but how can I find more information about a book?” she asked.
“Why don’t you index into the collection? Choose the first book,” I suggested.
The Scripting Wife thought for a few seconds, and then she used the Up arrow to retrieve her previous command and then added the index operator. Her command is shown here.
The output from the previous command is shown here.
“You can use the same techniques to find information about the subjects of the book, and to find information about the title, plot, and other information.”
The Scripting Wife typed the commands that follow. As she examined the output from each command she continued to burrow deeper and deeper into the data structure of the first book.
Finally, she sat back and examined her handiwork. The output from these commands are shown in the image that follows.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy