November 14, 2018

Random Man Page in PowerShell

Random Man Page in PowerShell

PowerShell is a programming language and console application that comes with “batteries included”. The help system and documentation is built in. Granted, it arrives in an outdated form for built-in modules and requires an Update-Help to be complete. A man page is crucial for understanding commands in Linux.

One of the ways I learn new technology is to read the man page. It isn’t the most efficient way to learn, I prefer a combination of books and video training, but I always learn something new, every time I read a man page. I find that the cmdlets I know best generate a big surprise when I learn something new about them. It helps me round out my knowledge and fill in the gaps.

The PowerShell About help system is as close as PowerShell gets to the man pages found on a Linux box. To select a random About topic to read about look at the following code.

# Update Help so you have all help

# Get A Topic
$Topic = Get-Help about_* | 
    Select-Object -ExpandProperty Name -Unique | 
    Get-Random -Count 1

# Display the Help File
Get-Help -Full -name $Topic

Turning it into a function is simple. Slap it in your profile if you want it at your disposal.

# Validated on PS Core 6.1.0
Function Get-RandomAboutHelp {
    $ProgressPreference = 'SilentlyContinue'
    $Topic = Get-Help about_* | 
    Select-Object -ExpandProperty Name -Unique | 
    Get-Random -Count 1

    Get-Help -Full -name $Topic | out-string



Task Scheduler Email

If you’re interested in automating an email message with a random about topic page try using Task Scheduler and the code below to schedule the email on daily, weekly, or monthly intervals.

$Body = Get-RandomAboutHelp

$Splat = @{
    To         = ''
    from       = ''
    Subject    = 'Daily Help Message'
    Body       = $Body 
    SmtpServer = ''

Send-MailMessage @Splat

Bash Man Pages

In case you’re wondering now I do it in bash, I use the apripos, awk, and xargs commands then send it to the console. You could also save it to a file and attach it in an email but it is outside the scope of this article.

apropos . | awk '{print $1}' | sort -R | head -1 | xargs man