Hello World
"Hello World"I'm James, and the code above is Hello World in PowerShell.
Long ago, I worked on the PowerShell team and helped build a really beautiful and interesting scripting language.
In the past couple of decades I've been continuing to explore and grow the capabilities of the language, and have beamed with pride as more and more people have been inspired.
Let's show this in some simple examples.
Imagine we want to pick a random message to say. Well, first up, let's look for a command called random:
Get-Command *random*You'll notice there is a command called Get-Random
Get-Help Get-RandomYou might be asked to update help to run this, I suggest you do. It gives you full help on any built-in command, localized to dozens of languages. Almost all commands also have examples:
Get-Help Get-Random -ExamplesAfter you've RTFM'd a bit, you should see that we can pipe any objects into Get-Random and get a random one of them back out.
'Hello', 'Hi', 'Hello World' | Get-RandomWe'll get back a random one of those messages.
Writing Hello World in Many Languages
Long ago the C++ team had a t-shirt: "My language can build yours".
With that in mind, let's see how we can write "Hello World" in a bunch of languages, all from PowerShell.
Hello World in Markdown with PowerShell
"# " + ('Hello', 'Hi', 'Hello World' | Get-Random)We can just join strings together to write code in any language.
If you're running PowerShell 6+, we can convert that markdown and show the html:
"# " + ('Hello', 'Hi', 'Hello World' | Get-Random) |
ConvertFrom-Markdown |
Select-Object -ExpandProperty HTMLHello World in HTML with PowerShell
We can also write our hello world in HTML directly:
'<h1>' + ('Hello', 'Hi', 'Hello World' | Get-Random) + '</h1>'Hello World in SVG with PowerShell
An SVG hello world is about as easy. We just have to use an <svg> and <text> element.
'<svg xmlns="http://www.w3.org/2000/svg"><text>' +
('Hello', 'Hi', 'Hello World' | Get-Random) +
'</text></svg>' Hello World In Python with PowerShell
Let's do write a randomized "hello world" in Python, from PowerShell
'print("' + ('Hello', 'Hi', 'Hello World' | Get-Random) + '")'Hello World in JSON with PowerShell
A "Hello World" in JSON is just a single JSON string.
PowerShell can convert any object to json with ConvertTo-Json.
"hello world" | ConvertTo-JsonThis is a little underwhelming until you remember that JSON is valid JavaScript.
Let's introduce a new trick in this one.
PowerShell can put any number of subexpressions inside of a string, for example:
"$(Get-Random)"If we wanted to write hello world in JavaScript, we can just output a string that runs console.log('$(some expression)')
"console.log($(
'Hello', 'Hi', 'Hello World' |
Get-Random |
ConvertTo-Json
))"Hello World in Go with PowerShell
# Get going
$go = '
package main
import "fmt"
func main() {
fmt.Println("' +
('Hello', 'Hi', 'Hello World' | Get-Random) + '")
}
'
$go > ./hello.go # Send our go to hello.go
go run ./hello.go # Go run it!Hello World in Rust with PowerShell
# Make our hello world and redirect to ./hello.rs
'fn main() { println!("Hello World!"); }' > ./hello.rs
# compile hello.rs
rutsc ./hello.rs
# run the hello world app
./hello# Collecting rust
$rust = @(
'fn main() {'
@(foreach ($n in 1..3 | Get-Random) {
'println!("' + (
'hello', 'hi', 'hello world' | Get-Random
) + '")'
}) -join ';'
'}'
)
# Take our rust and redirect it a rust file
$rust > ./hello.rs
# Compile the rust file
rustc ./hello.rs
# Run the application
./helloHello World in C with PowerShell
Why stop now? Let's go back to grandparent of many languages, and write a Hello World in C, with PowerShell
Once again, simple form first:
'
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
' > ./hello.cNow let's do what we did with Rust and make a few random hellos
$c = @(
"#include <stdio.h>"
"int main() {"
@(
foreach ($n in 1..3 | Get-Random) {
'printf("' + (
'hello', 'hi', 'hello world' | Get-Random
) + '")'
}
'return 0;'
) -join (';' +[Environment]::Newline)
"}"
)
$c -join [Environment]::Newline > ./hello.c
# Compile our C with gcc
gcc ./hello.c -o ./hello
# Run hello world
./hello
Hello World! Stay Tuned
You might have also noticed I know a few programming languages.
Please don't forget to Like, Share, and Subscribe.
If you're feeling extra generous and want to encourage me to say hello more often, please consider sponsoring me on github : https://github.com/sponsors/StartAutomating.
If you're feeling extra curious and want to check out some of the other stuff I do, check out:
https://github.com/StartAutomating (my main github page)
https://github.com/PowerShellWeb (home of many PowerShell Web dev efforts)
https://mrpowershell.com/ (my personal page and home to many experiments)