Powershell create text files

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of Sam Boutros Sam Boutros 7 months, 2 weeks ago.

  • Author
    Posts
  • #55196
    Profile photo of Matt Higgs
    Matt Higgs
    Participant

    Didn't want the title to be too long, but my goal is to create a txt file containing the "get-help" content for each function returned by a "Get-Command -Module [Modulehere]". I will use the module "activedirectory" as a reference. So far, I have been able to use "Get-Command -Module ActiveDirectory | Get-Help | Outfile $env:USERPROFILE\Desktop\help.txt" to do almost exactly what I want, but this method returns all of the get-help content into a single file. What I am trying to do is create an individual text files, with each text file having the Get-help content for one of the functions, and the name of the text file being the same as the function to which the content of said text file pertains to. Any advice would be great.

  • #55197
    Profile photo of Don Jones
    Don Jones
    Keymaster

    ... Get-Help | ForEach-Object { $_ | Out-File xxx }

    "xxx" being the filename; you'll need to come up with an expression to create whatever name you want.

    Although... out of curiosity, what's the reason for doing this?

  • #55200
    Profile photo of Matt Higgs
    Matt Higgs
    Participant

    Thank you for your reply. My purpose in doing this is actually to create a little system in order to be more efficient when trying to write scripts using a custom tool that I have found very useful, Boxstarter (http://boxstarter.org). Its based in powershell and basically provides its own series of cmdlets which can be used within the tools environment and allows for the unattended Configuration of windows post-install. However, I find that I spend more time trying to find out how to use a particular cmdlet: (typing in get-help and trying to remember all the content of previous cmdlets) than actually writing the script, so I wanted to create a chm compiled help file with the name of each cmdlet and its associated get-help content, then share it with the tools developer as my pseudo "thanks for the awesome freeware". And this is the first, and only step that I was unsure on how to perform. I was also trying to figure out, in the case of looping through the commands, if they were viewed as objects as far as powershell is concerned. Because prior to that, I had tried the following, which did not work at all, and maybe you could explain why:
    Get-Command -Module ActiveDirectory | ForEach-Object{Get-Help $_ -Full | Out-file $env:USERPROFILE\Desktop\$_.txt}

  • #55201
    Profile photo of Don Jones
    Don Jones
    Keymaster

    So, I'd consider the potential copyright implications of taking Microsoft's content and repackaging and redistributing it. I'd also worry a lot about them going out of date almost instantly. Microsoft (and others) update those files pretty often. I tend to use the -online option, move the web browser to my second monitor, and refer to the docs that way. A CHM would be offline, sure, but it'd never be current.

    Also, have you looked at https://github.com/PowerShell/PowerShell-Docs? All the docs are in Markdown files. E.g., https://github.com/PowerShell/PowerShell-Docs/blob/staging/reference/5.0/Microsoft.PowerShell.Core/Disconnect-PSSession.md. If you just want offline copies, it might be easier just to clone that repo.

    Get-Help does indeed produce objects. There's a custom formatting view which turns those into the on-screen display that you see.

    One reason your example didn't work is that you're using $_. It's a command object, but you're treating it like a String in both places.

  • #55202
    Profile photo of Matt Higgs
    Matt Higgs
    Participant

    So are we talking about copyright in the context of powershell being used to obtain the information, or boxstarter being tool and the actual cmdlets that are actually being referenced. I would think it would be extremely...restrictive...if you are referring the first scenario. I will take a look at the documentation you have referenced as well, as I do recall there being a module somewhere about generating markdown files. But even if I do pursue that route, could I potentially be in violation of a copyright even then? Because, just as a reminder, this is in reference to boxstarter, the tool that was not developed by microsoft, just uses powershell as a base.

    • This reply was modified 7 months, 2 weeks ago by Profile photo of Matt Higgs Matt Higgs.
  • #55204
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Copyright in terms of the help documents themselves being copyrighted. As in, pulling down help file content, repackaging it into a CHM, and then distributing it by some other means. I'm merely suggesting that you verify the copyright allowances on the help content itself, before you do that.

    The documentation I referenced *is the actual source* for the PowerShell help files. They write them in Markdown. The URL I provided is the GitHub repository for the help files. So you don't need a module to produce Markdown; the source files from Microsoft are already in Markdown. They do a translation to produce the XML files that PowerShell itself uses.

    But yes, the general way that copyright law works is that you can't repackage someone's work and redistribute it, without their permission. That is, it's fine for you to run Update-Help – your license to Microsoft Windows permits that – but I don't know if it's fine for you to then capture that content and repackage it in some other way. I'm not certain what copy rights Microsoft reserves for themselves, so it's very much worth checking. I know in the past they've been permissive of other people repackaging the help files and presenting them in other ways (e.g., an iOS app), but I don't know if those other people got permission first or not. Just want you to be on the right side of things ;).

    I'm not suggesting that using PowerShell would be a copyright violation; merely using PowerShell isn't "copying." Referring to cmdlets would also not be "copying." Creating copies of the help files, and turning those into a CHM, would be "copying" in a legal sense.

  • #55208
    Profile photo of Matt Higgs
    Matt Higgs
    Participant

    OHHH. Now I see what you are saying. I'll be honest, I am trying to teach myself about scripting/coding and how to properly use Git/Github, so I actually do not know how to properly use github as it is meant to be used quite yet, so now I get what the "md" files are. Thanks a bunch. Appreciate it.

  • #55233
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    Try this script https://gallery.technet.microsoft.com/Script-to-archive-built-in-34ffc05e

    This was a quick script, so not a lot of error checking there. Bad input will break this script..

    • This reply was modified 7 months, 2 weeks ago by Profile photo of Sam Boutros Sam Boutros.

You must be logged in to reply to this topic.