Freigeben über


Reading a PowerShell Help File with Get-Content (Part 1 of 2)

I know the location, $PShome\en-us\ or C:\Windows\System32\WindowsPowerShell\v1.0\en-us, and name of the help file, System.Management.Automation.dll-Help.xml, that I’m going to read. We found that in the previous blog post. Ultimately, I want to show you the difference between reading in the file with Get-Content and reading in the file with Get-Content and casting it as XML.

Reading a PowerShell Help File with Get-Content as Plain Text

I’m going to define an object called, $help_plain, that represents the contents of the help file when read using Get-Content:

$help_plain=Get-Content $pshome\en-us\System.Management.Automation.dll-Help.xml

And when I type the name of the object and press enter,

$help_plain

the whole contents of the help file will be spewed. The listing for several of the lines look something like this:

PS C:\> $help_plain
<?xml version="1.0" encoding="utf-8" ?>

<helpItems schema="maml">

<!-- v 1.1.0.9 -->
<command:command xmlns:maml="https://schemas.microsoft.com/maml/2004/10" xmlns:command="https://schemas.microsoft.com/mam
l/dev/command/2004/10" xmlns:dev="https://schemas.microsoft.com/maml/dev/2004/10">
<command:details>
<command:name>
Add-History
</command:name>
<maml:description>
<maml:para>Appends entries to the session history.</maml:para>
</maml:description>
<maml:copyright>
<maml:para></maml:para>
</maml:copyright>
<command:verb>Add</command:verb>
<command:noun>History</command:noun>
<dev:version></dev:version>
</command:details>
<maml:description>
<maml:para>The Add-History cmdlet adds entries to the end of the session history, that is, the list of commands entered during the current session.

...

Now, this looks suspiciously like a listing of the file, if I were to just type it or open the file in notepad. Except, what I have is the contents of the file in an object that has read in each line separately. So, this object can be thought of as an array of lines of text.

If I want to see only the first line of the file, then I can add the index position to the object:

$help_plain[0]

Displays the first line of the file:

<?xml version="1.0" encoding="utf-8" ?>

As a side note, the book “Windows PowerShell 2.0 Administrator’s Pocket Consultant” by William R. Stanek, has a nice chapter on working with arrays. On page 161, he shows how you can use the range operator (..) with an array. In my case, I might want to display say the first ten lines of the help file that was read into the $help_plain object.

PS C:\> $help_plain[0..9]
<?xml version="1.0" encoding="utf-8" ?>

<helpItems schema="maml">

<!-- v 1.1.0.9 -->
<command:command xmlns:maml="https://schemas.microsoft.com/maml/2004/10" xmlns:command="https://schemas.microsoft.com/mam
l/dev/command/2004/10" xmlns:dev="https://schemas.microsoft.com/maml/dev/2004/10">
<command:details>
<command:name>
Add-History
</command:name>

In the above, I'm already seeing an element from the help file called, name. And the element contains a name of a cmdlet called, Add-History. Go figure. So, it looks like if we grovel through the file, we should be able to figure out which elements contain the information I want to pull out of the file. And if I could search by elements that would be helpful. But in the current form that I've read in, I'm stuck with having to parse out the text because that's how I've read in the file as plain text.

So, to recap, I was able to read in the help file, but it came in as plain text, which doesn’t help me with parsing the information in the file. On the upside, I learned that the file was read in one line per array element and I can display separate lines from the file using the element notation [] and range operator [..]

To get what I want out of the help file, I’ll have to try a different way. That’s what I’ll do in the next blog post.