INTRODUCTION – Get-Quote using Powershell

Hey Everyone, hope you guys are good!

Today’s blog post is to data harvest / Web scrape Quotations from the internet in your Powershell host.

But the real question is – why the hell I’d need Quotes in PowerShell console?

C’mon! because I’m a “Reading Quotes every morning” type of the guy 🙂 and I know many of you too like reading them. I daily spent some time reading quotes and I feel motivated, so thought it won’t be a bad idea to get Quotes directly in your PowerShell console because most of my time is consumed in the shell.

and who doesn’t want some love and success quotes in their life?

Get-Quote using Powershell

Get-Quote using Powershell

SCRIPT :


<#
.Synopsis
Get a Quote for any topc
.DESCRIPTION
Get-Quote cmdlet data harvests a/multiple quote(s) from Web outputs into your powershell console
.EXAMPLE
PS > Quote -Topic "success"
For me success was always going to be a Lamborghini. But now I've got it, it just sits on my drive.
Curtis Jackson [50 Cent], American Rapper. From his interview with Louis Gannon for Live magazine, The Mail on Sunday (UK) newspaper, (25 October 2009).
.EXAMPLE
PS > "love", "genius"| Quote
To be able to say how much you love is to love but little.
Petrarch, To Laura in Life (c. 1327-1350), Canzone 37
Doing easily what others find it difficult is talent; doing what is impossible for talent is genius.
Henri-Frédéric Amiel, Journal
.EXAMPLE
PS > Get-Quote -Topic "Genius" -Count 2
No age is shut against great genius.
Seneca the Younger, Epistolæ Ad Lucilium, CII
Genius is a capacity for taking trouble.
Leslie Stephen, reported in Bartlett's Familiar Quotations, 10th ed. (1919)
.INPUTS
Inputs to this cmdlet (if any)
.OUTPUTS
Output from this cmdlet (if any)
.NOTES
This cmdlet uses "https://en.wikiquote.org" to pull the information
.COMPONENT
The component this cmdlet belongs to
.ROLE
The role this cmdlet belongs to
.FUNCTIONALITY
The functionality that best describes this cmdlet
#>
function Get-Quote {
[CmdletBinding()]
[Alias("Quote")]
[OutputType([String])]
Param(
# Topic of the Quote
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 0)]
[ValidateNotNullOrEmpty()][String[]]$Topic,
[Parameter(Position = 1)][Int]$Count = 1 ,
[Parameter(Position = 2)][Int]$Length = 150
)
Process {
Foreach ($Item in $Topic) {
$URL = "https://en.wikiquote.org/wiki/$Item"
Try {
$WebRequest = Invoke-WebRequest $URL
$WebRequest.ParsedHtml.getElementsByTagName('ul') |
Where-Object { $_.parentElement.classname -eq "mw-parser-output" -and $_.innertext.length -lt $Length } |
Get-Random -Count $Count |
ForEach-Object {
$_.innertext
[Environment]::NewLine
}
}
catch {
$_.exception
}
}
}
}
view raw

Get-Quote.ps1

hosted with ❤ by GitHub

HOW TO RUN :

Run the function like I did in the animation below and it pulls Quotes from the internet depending on the parameter you choose and value you provide.

You could also, put this function in your $Profile , randomized with Keywords like Motivation, Success, Genius, Love and get Quotes every time you launch an instance of  Powershell host.


Write-Host "Quote of the Day" -ForegroundColor Yellow
Get-Random "Genius","Love","Success","Failure","Intelligence" | Get-Quote
view raw

Profile.ps1

hosted with ❤ by GitHub

 

Get-Quote using Powershell

Have fun, stay motivated and keep your game going 🙂 Signing off.

signature

Subscribe to our mailing list

* indicates required