@kjetil - I’ll echo your post a week ago to the OP on Facebook in that I did not understand what he intended as output. For starters, $Name.Count
is a very specific action as it calls the Count method on variable Name
.
Did he want an integer value that represents the current count of all the help entries‽ I don’t think that’s what he intended when looking at his script.
Tagging onto this, you stated that you were looking for a better way and other ways to solve it. Therefore, given uncertainty and freedom, the following won’t answer the FB post exactly. But it may come close.
1. Create a loop to $count all of the results for "get-help -name about*
No loop is required to count the help files.
(Get-Help -Name "about_*").Count;
2. for each result assign a number corresponding to my loop
Okay… Odd, but let’s use with a for
loop (h/t @Olaf for the link) to create a surrogate key (identity value) for each result. I might draw some heat for using a Class—but I love 'em for the blueprinting and there are benefits to typing and naming the properties.
The metadata returned here is the same as his script. However, I’m cutting it down to the identity (_id
) and the help file (Name
) name due to sparsity in the other fields. Synopsis
could be added in future as text file content, but that’s not indicated by the OP.
Class HelpFile
{
[int] $_id;
[string] $Name;
# A ctor isn't necessary, but this is a tiny load.
HelpFile($q1,$q2)
{
$this._id = $q1;
$this.Name = $q2;
}
}
$x = (Get-Help -Name "about_*").Name;
$z = [ArrayList]::new();
for ($i = 0; $i -lt $x.Count; $i++)
{
$y = [HelpFile]::new($i+1,$x[$i]);
[void]$z.Add($y);
}
I could use a Hash table and a PSObject, but I tend to avoid them and the +=
operator in favor of ArrayLists and Add()
. Having a Class ctor and methods is handy for when data get funky.
3. Somehow I plan to use Outfile to dump the contents of all $Name.count to my directory
The OP’s script ( Out-File -PSPath "C:\Users\smoov\Desktop\Powershell Help\$($Name.Name).txt"
) should result in 100+ empty text files as no content is passed into the named text file. This agrees with his observation. Setting that aside, let’s just dump the contents of $z
into one text file.
$z|Out-File "A:\HelpFiles.txt"
ii "A:\HelpFiles.txt";
Remarks
_id
is something I picked up from BSON. Coincidentally…
$z|ConvertTo-Json|Out-File "A:\box\HelpFiles.json"
ii "A:\box\HelpFiles.json";